Why is onKey() called twice?
OnKey is fired twice: the first time for key down, and the second time for key up, so you have to filter:
YOUR_VIEW.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//This is the filter
if (event.getAction()!=KeyEvent.ACTION_DOWN)
return true;
switch (keyCode) {
case KeyEvent.KEYCODE_1 :
MakeToast(1);
break;
case KeyEvent.KEYCODE_2 :
MakeToast(2);
break;
case KeyEvent.KEYCODE_3 :
MakeToast(3);
break;
}
return true;
}
});
KeyEvent.KEYCODE_BACK getting called twice in Fragment
It is correct that onKey would be called twice, one for a Down event and another one for an Up event. Please try to add a condition:
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
...
}
Hope it helps.
My Button is always called twice with performClick
As there are two actions on press
KeyEvent.ACTION_DOWN
&KeyEvent.ACTION_UP
http://developer.android.com/reference/android/view/View.OnKeyListener.html
Returns true
if the listener has consumed the event, false
otherwise.
Try it...
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
if(keyCode == KeyEvent.KEYCODE_ENTER)
{
recherche.performClick(); // recherche is my button
return true;
}
}
return false;
}
Enter key on EditText hitting onKey twice
Ahhhh
I think this is happening for key up and key down?
Why do I have to press the key twice?
The onKeyDown
event happens before the character is actually appended to the textbox.
To capture the new character use onkeyup
event instead.
Anyway, your code is currently not cross browser.. window.event
is not standard instead pass the event as argument to the function like this:
onKeyPress="return submitenter(event)"
Then in the function:
function submitenter(evt){
//IE fix
if (typeof evt == "undefined")
evt = window.event;
var keycode = evt.keyCode || evt.which;
...
Why onKey event double processing in Android?
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.
}
Related Topics
How to Search for a Value in Firebase Android
How to Draw Dashed Polyline with Android Google Map Sdk V2
Firebase Authentication Not Working in Signed APK
Android - How to Intercept the 'Install Application' Intent
Firebase Dynamic Link Not Opening the App
Clear Application Cache on Exit in Android
Listening to Scroll Events Horizontalscrollview Android
How to Store Large Blobs in an Android Content Provider
Google Geocoder Service Is Unavaliable (Coordinates to Address)
Capturing Images with Mediastore.Action_Image_Capture Intent in Android
Opening Infowindow Automatically When Adding Marker Google Maps V2 Android
How to Detect When the User Launches Another App? (Android)
Onserviceconnected Never Called After Bindservice Method
Launching an Android Application from the Browser