KeyEvents - shiraji/androidannotations GitHub Wiki

Since AndroidAnnotations 4.0

You can easily handle the four key events in an enhanced class which implements the KeyEvent.Callback interface. For some possible classes read the KeyEvent.Callback documentation.

The four event annotations are

At all annotations you can set the key code or key codes by the annotation parameter. Easily pass codes by using KeyEvent.KEYCODE_* constants.
For example KeyDown(KeyEvent.KEYCODE_ENTER) or KeyDown({ KeyEvent.KEYCODE_0, KeyEvent.KEYCODE_1 }).

If the key code is not set, the name of the method will be used to guess the key code. Possible naming conventions are enter, onEnter, enterPressed, onEnterPressed which will be called if Enter key's corresponding action is called.

All annotated methods may return void, boolean or Boolean. If returning void, it will be considered as returning true (ie: the method has handled the event).

##@KeyDown, @KeyUp, @KeyLongPress

The method may have zero or one parameter and this parameter can only be a KeyEvent (the event that was triggered). The method must not be private. Two different methods cannot handle the same key code in the same class.

Usage example:

@EActivity
public class MyActivity extends Activity {

    @KeyDown
    void enterPressed() {
        //...
    }

    @KeyUp(KeyEvent.KEYCODE_ESCAPE)
    boolean handleEscapeActionUpEvent() {
        //...
        return false;
    }

    @KeyLongPress({ KeyEvent.KEYCODE_F, KeyEvent.KEYCODE_G })
    void fOrGKeyLongPress(KeyEvent keyEvent) {
        //...
    }
}

##@KeyMultiple

The method may have zero, one or two parameters and these parameters can be

  • a int or Integer (the count of action repeates) and/or
  • a KeyEvent (the event that was triggered).

The method must not be private. Two different methods cannot handle the same key code in the same class.

Usage example:

@EActivity
public class MyActivity extends Activity {

    @KeyMultiple
    void onEnterPressed() {
        //...
    }

    @KeyMultiple(KeyEvent.KEYCODE_ESCAPE)
    boolean handleEscapeActionMultipleEvent(int count) {
        //...
        return false;
    }

    @KeyMultiple({ KeyEvent.KEYCODE_F, KeyEvent.KEYCODE_G })
    void fOrGKeyWasMultiplePressed(int count, KeyEvent keyEvent) {
        //...
    }
}
⚠️ **GitHub.com Fallback** ⚠️