I've attached an OnKeyListener to an EditText. I've overrode the onKey handler to capture a KeyEvent.
When a user hits the enter key (either their computer enter key while using the emulator, or the hardware enter key on their phone with a hardware keyboard), the onKey handler gets executed twice. Both executions have the keyCode 66.
Why is this happening?
I want my screen so when the user hits the enter key, a search is performed. Because of what is happening, the search is needlessly happening twice.
My method looks like this:
mFilter.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
// perform search
return true;
}
return false;
}
});
This event is fired by
KeyEvent.ACTION_DOWN
andKeyEvent.ACTION_UP
. I have done debugging and finally I realize that there is an param calledKeyEvent event
that I never use, then I checked and found the problem.Ahhhh
I think this is happening for key up and key down?
I had the same issue and the answers above helped me but I am using Xamarin.Android (c#) so it is slightly different syntax.. Here is what worked for me:
This way, the DoSomething() would only be called on hitting Enter Key (Up) only and thus would be fired once. Works and tested on Xamarin.Android
Try this:
you can filter like this :
idem when you push the key with KeyEvent.ACTION_DOWN