Android - getting soft keyboard keys

I try to press a key on a soft keyboard, but I cannot do this. I am currently using the following code

@Override
public boolean dispatchKeyEvent(KeyEvent KEvent) 
{
int keyaction = KEvent.getAction();

if(keyaction == KeyEvent.ACTION_DOWN)
{
    int keycode = KEvent.getKeyCode();
    int keyunicode = KEvent.getUnicodeChar(KEvent.getMetaState() );
    char character = (char) keyunicode;

    System.out.println("DEBUG MESSAGE KEY=" + character + " KEYCODE=" +  keycode);
}


return super.dispatchKeyEvent(KEvent);

}

It captures key events for the hardware keyboard, but not for the virtual one. Can someone help me.

0
source share
2 answers

From the official Android page

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

, TextWatcher , SoftKeyboard, :

myEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {


        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            // TODO Auto-generated method stub
        }
    });
+2

:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 1) { 
        finish();
        return true; 
    }

    return super.onKeyDown(keyCode, event);
}
0

All Articles