9

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.

arnp
  • 3,178
  • 6
  • 26
  • 43

4 Answers4

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); }
Ronak Mehta
  • 5,971
  • 5
  • 42
  • 69
  • 2
    only works if no text entered to the EditText and I am pressing the backslash button – narancs Aug 09 '17 at 19:26
  • 2
    This may not work, because software keyboards don't have to send KeyEvents (some keyboards send none, Gboard sends only ASCII keys). – Lev Leontev Jun 09 '19 at 09:58
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

phnmnn
  • 12,813
  • 11
  • 47
  • 64
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);
    }
Nadeem Iqbal
  • 2,357
  • 1
  • 28
  • 43
nikki
  • 3,248
  • 3
  • 24
  • 29
0
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // Do Code here
    }
    return super.onKeyDown(keyCode, event);
}
Krishnakant Dalal
  • 3,568
  • 7
  • 34
  • 62