Enter key on EditText hitting onKey twice - android

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;
}
});

Ahhhh
I think this is happening for key up and key down?

if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction()==0) {

Try this:
mFilter.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
// perform search
return true;
}
}
return false;
}
});

you can filter like this :
object.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
// do stuff
return true;
}
return false;
}
});
idem when you push the key with KeyEvent.ACTION_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:
MyStringTextBox.KeyPressed += OnEnterKeyPressed;
protected void OnEnterKeyPressed(object sender, View.KeyEventArgs e)
{
if (e.KeyCode == Keycode.Enter && e.Event.Action == KeyEventActions.Up)
{
DoSomething(this, EventArgs.Empty);
}
else
{
e.Handled = false;
}
}
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

This event is fired by KeyEvent.ACTION_DOWN and KeyEvent.ACTION_UP.
I have done debugging and finally I realize that there is an param called KeyEvent event that I never use, then I checked and found the problem.

I debugged and what worked for me was that,
editText.setOnKeyListener(View.OnKeyListener { view, i, keyEvent ->
if (i == KeyEvent.KEYCODE_ENTER && enterAsSend && (keyEvent.action == KeyEvent.ACTION_UP || keyEvent.action == KeyEvent.ACTION_DOWN)) {
//Do stuff
}
return#OnKeyListener true
}
false
})
and checkout your Editext that android:inputType="textNoSuggestions" because the first click of enter key gives us the suggestion from the dictionary.

Related

onKeyDown - Control key is not working in Activity

I am trying to add shortcuts in my app when external keyboard is connected. SHIFT+KEY, this combination is triggering the following event. But Control+KEY and Control key events are not triggered. Can anyone suggest me on this.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_CTRL_LEFT) {
flag_sellreceipt = KeyEvent.isModifierKey(KeyEvent.KEYCODE_CTRL_LEFT);
int i = KeyEvent.META_CTRL_LEFT_ON;
System.out.println("ctrl is pressed");
}
return
super.onKeyDown(keyCode, event);
}
Note: I have tested this in emulator.

Android Google Keyboard crashes when capturing KeyEvent.KEYCODE_ENTER

I'm trying to manage the Next event on the Google Keyboard on a Google Nexus 5. I want my application to check user information when the Next button gets pressed.
The code looks like this:
private TextView.OnEditorActionListener GenerateEditorListeners()
{
return new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_NULL && event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if (!finished)
{
if (CheckUserInfo()) finished = true;
}
return true;
}
else
{
return false;
}
}
};
}
On a Samsung Galaxy S4 works perfect, but on a Google Nexus 5 the actionId = 0 and the event = null. So I figure out that the Samsung keyboard works fine with this code, but doesn't happen the same with the Google Keyboard.
Any idea on why it's not wokring for Google's keyboards?
EDIT: I've read in this post that Google keyboard has a bug in some LatinIME keyboards.... I'm using a latin one. If that's the problem, how to solve it?
I could solve it by using the OnKeyListener event:
textUserEmail.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if (!finished)
{
if (CheckUserInfo()) finished = true;
}
return true;
}
else
{
return false;
}
}
});
Now it works in all keyboards.

Prevent KeyEvent - Keycode Enter

I have an physical keyboard connected to my android device. I have an application with two buttons and the following two functions handle the KeyEvent:
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(state==State.INI){
char unicodeChar = (char)event.getUnicodeChar();
Log.d("CHAR", "UP: "+Character.toString(unicodeChar)+" - "+Integer.toString(keyCode));
}
return true;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(state==State.INI){
char unicodeChar = (char)event.getUnicodeChar();
Log.d("CHAR", "DOWN: "+Character.toString(unicodeChar)+" - "+Integer.toString(keyCode));
}
return true;
}
but even it handle the ENTER key doesn't prevent it to be handled by the system :s Per example, i changed the return true to false and i can navigate with the arrow keys between the buttons so with return true it is working for almost every key except ENTER :s
Resolved with:
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
Log.d("CHAR","YOU CLICKED ENTER KEY");
return false;
}
return super.dispatchKeyEvent(e);
};

how to add special character when the next line key is pressed?

I want to add a special character when the nextline button(key) is pressed in android mobile.
Plz help me to handle the Android 2.2 keyboard.
Am trying to use this code but its not supported me...
etDetail.addKeyPressHandler(new KeyDownHandler() {
public void onKeyPress(KeyPressEvent event) {
int charCode = event.getUnicodeCharCode();
if (charCode == 0) {
// it's probably Firefox
int keyCode = event.getNativeEvent().getKeyCode();
// beware! keyCode=40 means "down arrow", while charCode=40 means '('
// always check the keyCode against a list of "known to be buggy" codes!
if (keyCode == KeyCodes.KEY_ENTER) {
doOnEnterKeyPressed();
}
} else if (charCode == 13) {
doOnEnterKeyPressed();
}
}
});
What packages are added to do this task or give some ideas
Thanks in advance
Please try this. I hope this code snippet will help you
editText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
doOnEnterKeyPressed();
}
return true;
}
});

Backwards compatibility of onBackPressed

If I use onBackPressed() on Android 1.5, my application crashes. Is there any possibility to deactivate this function if running on an Android 1.5 device?
The code there is not absolute necessary but a real "nice to have", so I would like to keep it on newer devices and just drop it on older ones.
Is this possible?
edit: I think I found it, just the old way:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
return true;
}
return super.onKeyDown(keyCode, event);
}
You can try to detect runtime which version of SDK using your application and depending on that prepare different branches. Like:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(Build.VERSION.SDK_INT==Build.VERSION_CODES.CUPCAKE) //if it's 1.5
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{ // do something on back.
return true;
}
}
return super.onKeyDown(keyCode, event);
}

Categories

Resources