Android Google Keyboard crashes when capturing KeyEvent.KEYCODE_ENTER - android

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.

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.

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

WebViewClient history wrong on HoneyComb

I have a WebViewClient in an Activity in which I override the onKeyDown method like this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
} else {
finish();
return true;
}
}
This works like a charm on my phone as well as the emulators I tested on including a 3.0 emulator.
Weird thing is that on a 3.1 emulator as well as on my Xoom tablet (3.0.1) it does NOT work. It seems that webView.canGoBack() always returns true on these platforms.
Questions:
Has anybody else found similar behaviour?
Do you have a workaround/hack that allows me to make the backbutton work to navigate in the web view history as well as ultimately out of the activity if required?
Update: I have since then change the app to use fragments with the compatbility library so I am now using this:
webView.setOnKeyListener(
new View.OnKeyListener() {
#Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
} else {
redirectHelper.finish();
return true;
}
}
}
);
where redirect helper basically is a wrapper for proper finishing of an activity or removing a fragment from the stack. Still has the same issue though..
I'm using this without issues on 3.1 and Galaxy Tab 10.1. Haven't tried onKeyDown method.
#Override
public void onBackPressed() {
if( webView.canGoBack() ) {
webView.goBack();
} else {
super.onBackPressed();
}
}

Enter key on EditText hitting onKey twice

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.

Categories

Resources