10

I need a listener to identify the keypress in the soft keyboard/on screen keyboard.

I tried with addtextchangelistener textwatcher but this one give the good result but it shows the change also when some text is pasted into it.

I need to identify only the key press by the user.

Is there any possible way to detect the key press.

4 Answers 4

3

see this keyevent and use following code to identify which key is pressed by Users.

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { // Do Code here } else if(keyCode == KeyEvent.KEYCODE_0) { } else if(keyCode == KeyEvent.KEYCODE_1) { } return super.onKeyDown(keyCode, event); } 
Sign up to request clarification or add additional context in comments.

2 Comments

only works if no text entered to the EditText and I am pressing the backslash button
This may not work, because software keyboards don't have to send KeyEvents (some keyboards send none, Gboard sends only ASCII keys).
3

When handling keyboard events with the KeyEvent class and related APIs, you should expect that such keyboard events come only from a hardware keyboard. You should never rely on receiving key events for any key on a soft input method (an on-screen keyboard).

see: Handling Keyboard Actions

2 Comments

the guy is asking about the soft keyboard, not the hardware keyboard
You should never rely on receiving key events for any key on a soft input method (an on-screen keyboard)
1

See this if can help you.

@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 1) { finish(); return true; } return super.onKeyDown(keyCode, event); } 

Comments

0
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { // Do Code here } return super.onKeyDown(keyCode, event); } 

3 Comments

This is to identify the back keypress.
you can identify any key available on keyboard.
I have already tried onKeyDown listens only for the soft keys and not in the keyboard

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.