Pyae Phyoe Shein
Pyae Phyoe Shein

Reputation: 13827

Why onKey event double processing in Android?

I wonder why onKey event is double processing when I logging it. Here is my coding.

ed1.setOnKeyListener(new View.OnKeyListener()
{
    Integer count = 0;
    String typeWord = "";
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {                   
        count++;
        char c = (char)event.getUnicodeChar();
        typeWord = typeWord + c;                    
            Log.v ("keypress", "Keycode " + typeWord + " - skeyCode " + count);                     
        return false;
    }
});

And here is logging of when I type "a".

10-16 02:38:27.025: VERBOSE/keypress(362): Keycode a - skeyCode 5
10-16 02:38:27.315: VERBOSE/keypress(362): Keycode aa - skeyCode 6

Upvotes: 1

Views: 533

Answers (1)

Yashwanth Kumar
Yashwanth Kumar

Reputation: 29121

It is because ,it is detecting both key_down and key_release events, so it is firing the same code twice.

you have to select where you want to process code on key_down or key_release

if (KeyEvent.ACTION_DOWN == event.getAction()) {
    // code to be executed on key press.
}

Upvotes: 4

Related Questions