Difference between Android KeyEvent and keycode

user3642735 picture user3642735 · Aug 2, 2014 · Viewed 15.9k times · Source

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?

Answer

Jorgesys picture Jorgesys · Aug 2, 2014

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 );
  }