2

I have an EditText with android:inputType="phone" keybord. There is an "OK" on this keyboard. I like to catch pressing this OK btn. However I don't know the key event. I would like to use code like this:

 setOnKeyListener(new OnKeyListener()
            {
                public boolean onKey(View v, int keyCode, KeyEvent event)
                {
                    if (event.getAction() == KeyEvent.ACTION_DOWN)
                    {
                        switch (keyCode)
                        {
                            case KeyEvent.KEYCODE_DPAD_CENTER:
                            case KeyEvent.KEYCODE_ENTER:
                            case KeyEvent.?????????? <- this one please

                                return true;
                            default:
                                break;
                        }
                    }
                    return false;
                }
            });

I hope the code is different from pressing the "round-arrow"!

I checked all the key events in the docs but could not find anything. Thanks

user387184
  • 10,953
  • 12
  • 77
  • 147
  • I'm not sure that this can be done, but take a look at [How to catch key events while soft keyboard is open android?][1]. [1]: http://stackoverflow.com/questions/6883334/how-to-catch-key-events-while-soft-keyboard-is-open-android – cornbread ninja Oct 13 '11 at 15:29
  • You could use a simple `System.out.println` on the event's keyCode, and look in the docs for it's relative constant. – Draiken Oct 13 '11 at 15:31
  • If I just put a Log there I get back 66, does that mean it is 66 on all Android phones or could it differ? Ps Draiken, I am not sure what you mean with "relative constant"? – user387184 Oct 13 '11 at 16:02

1 Answers1

6

The 'OK' button is usually associated with finishing the input, and is different from the others. Use setOnEditorActionListener to catch it:

editText.setOnEditorActionListener( new OnEditorActionListener()
    {
      public boolean onEditorAction( TextView v, int actionId, KeyEvent event )
      {
        // Do what you want to do here
      }
    });
dragonroot
  • 5,653
  • 3
  • 38
  • 63
  • thanks, I tried that but for some reason nothing works anymore. ie the other listeners do not react and the setOnEditorActionListener does not seem to catch anything (it does not even print a Log.i in the body) – user387184 Oct 13 '11 at 18:16