How to listen the keypress in the soft keyboard?

2020-02-06 07:06发布

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条回答
家丑人穷心不美
2楼-- · 2020-02-06 07:21

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); }
查看更多
爷的心禁止访问
3楼-- · 2020-02-06 07:28

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

查看更多
Explosion°爆炸
4楼-- · 2020-02-06 07:39
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);
    }
查看更多
爷、活的狠高调
5楼-- · 2020-02-06 07:45
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // Do Code here
    }
    return super.onKeyDown(keyCode, event);
}
查看更多
登录 后发表回答