Search code examples
androidandroid-edittextkeyeventandroid-keypad

error in key event handler


I tried to capture key event for android. Here is the source code i tried:

text.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {
                if(keyCode==KeyEvent.KEYCODE_A){
                    Toast.makeText(MainActivity.this, "A Pressed!!", Toast.LENGTH_LONG).show();
                }
                return false;
            }
        });

But it's doesn't work or show any error. How can i handle the key event for android keyboard?


Solution

  • Software keyboards don't generate onKey events. Only hardware keyboards do. If you want to see changes from a software keyboard, use a TextWatcher on the view.