Renjith
Renjith

Reputation: 3617

Disable DPAD keys in android

I am trying to catch events generated by the arrow keys (UP, DOWN, RIGHT and LEFT) and disable them. Below code snippet is from one of the activity class.

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.KEYCODE_DPAD_DOWN) return true;
    else return true;
}

However, with those code in place, key navigation is working. I tried adding key listener to activity which doesn't work either.

The target device is Samsung GT-I5500 with Android 2.2 version on.

Am I missing anything?

Upvotes: 4

Views: 2838

Answers (2)

alexandr.opara
alexandr.opara

Reputation: 454

Override onKeyDown also and return true and not false. Somnething like this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
        case KeyEvent.KEYCODE_DPAD_RIGHT:
        case KeyEvent.KEYCODE_DPAD_UP:
        case KeyEvent.KEYCODE_DPAD_DOWN:
            return true; 
    }
    return false;
}

Upvotes: 7

nicopico
nicopico

Reputation: 3636

In the documentation, it is stated that you should return:

  • true if you handled the event
  • false if if you want to allow the event to be handled by the next receiver.

Your method is returning false, so you are passing the event to the default key handler

Upvotes: 0

Related Questions