Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dispatchKeyEvent calling method twice

Tags:

java

android

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);
};
like image 931
Sorin Grecu Avatar asked Jun 20 '13 17:06

Sorin Grecu


1 Answers

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);
};
like image 193
Sorin Grecu Avatar answered Oct 12 '22 02:10

Sorin Grecu