4
votes

What is the difference? public boolean onKeyDown (int keyCode, KeyEvent event) The parameters are keyCode,the button,the user pressed,but what is that KeyEvent?

3

3 Answers

4
votes

Read the docs

Object used to report key and button events. Each key press is described by a sequence of key events. A key press starts with a key event with ACTION_DOWN. If the key is held sufficiently long that it repeats, then the initial down is followed additional key events with ACTION_DOWN and a non-zero value for getRepeatCount(). The last key event is a ACTION_UP for the key up. If the key press is canceled, the key up event will have the FLAG_CANCELED flag set.

6
votes

KeyEvent: Each key press is described by a sequence of key events, Key events are generally accompanied by a key code.

KeyCode: is a constant that define the event, and is defined inside KeyEvent Class.

For example if you want to detect the KeyEvent "ENTER" :

 @Override
  public boolean onKeyDown( int keyCode, KeyEvent event ) {
    if( keyCode == KeyEvent.KEYCODE_ENTER) {
      //Do something...
      return true; 
    }
    return super.onKeyDown( keyCode, event );
  }
3
votes

KeyEvent can be used to specify the detailed action, for example:

  • ACTION_DOWN the key has been pressed down but not released
  • ACTION_UP the key has just been released

Usage:

keyEvent.getAction() == KeyEvent.ACTION_DOWN