Sorin Grecu
Sorin Grecu

Reputation: 1034

dispatchKeyEvent calling method twice

I've implemented dispatchKeyEvent in my activity to listen to the Enter key being pressed. The problem is that when i click enter,it calls my method twice ? How can i fix this ? Thanks,have a nice day !

@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.KEYCODE_ENTER) {

              enter();
        return true;
    }
    return super.dispatchKeyEvent(e);
};

Upvotes: 13

Views: 7511

Answers (1)

Sorin Grecu
Sorin Grecu

Reputation: 1034

Fixed it,done this : At first i was doing ACTION_DOWN but that was triggering an older problem of mine.

 @Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
        if (event.getAction() == KeyEvent.ACTION_UP){

         enter();

            return true;
    }}
    return super.dispatchKeyEvent(event);
};

Upvotes: 26

Related Questions