How to listen the keypress in the soft keyboard?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-29 00:39:50

问题


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.


回答1:


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); }



回答2:


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




回答3:


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



回答4:


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);
    }


来源:https://stackoverflow.com/questions/10616567/how-to-listen-the-keypress-in-the-soft-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!