How to listen to a key press on a soft keyboard?

I need a listener to identify the keystroke on the keyboard / on-screen keyboard.

I tried to use textwatcher addtextchangelistener, but it gives a good result, but it shows the change also when some text is inserted into it.

I need to identify only the keystroke by the user.

Is there a way to detect a keystroke.

+4
source share
4 answers

see this keyevent and use the following code to determine which key users clicked.

@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); } 
+4
source
 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { // Do Code here } return super.onKeyDown(keyCode, event); } 
+1
source
 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); } 
+1
source

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

see Handling keyboard actions

0
source

Source: https://habr.com/ru/post/1412756/


All Articles