Difference between Android KeyEvent and keycode

What is the difference? public boolean onKeyDown (int keyCode, KeyEvent event) Parameters - keyCode, button, user pressed, but what is KeyEvent?

+4
source share
3 answers

Read docs

An object used to report key and button events. Each keystroke is described by a sequence of key events. A keystroke begins with a key event with ACTION_DOWN. If the key is held long enough to be repeated, then the initial snapshot follows additional key events with ACTION_DOWN and a non-zero value for getRepeatCount (). The last key event is ACTION_UP for the key up. If the keystroke is canceled, the FLAG_CANCELED flag will be set in the key event.

+4
source

KeyEvent : each keystroke is described by a sequence of key events, key events are usually accompanied by key code.

KeyCode: , KeyEvent .

, KeyEvent "ENTER":

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

KeyEvent can be used to indicate a detailed action, for example:

  • ACTION_DOWN key pressed but not released
  • ACTION_UP just released key

Using:

keyEvent.getAction() == KeyEvent.ACTION_DOWN
+3
source

All Articles