Android onKeyDown not catching Dpad center and enter key presses - android

Doing a simple override in my base activity of onKeyDown, I'm able to capture all key presses except those of the enter and dpad center buttons (as determined via breakpoint). I've no clue as to why - can anyone shed some light on the situation?
EDIT: Quick update - it does capture Dpad center and enter key LONG presses, but still not normal presses.

I know this question is already pretty old but in case some desperate coder got lost I post my answer.
I had a similar problem with my USB keyboard. When anything else except a EditText box was focussed the ENTER key was never caught by onKeyUp or onKeyDown.
if you use dispatchKeyEvent() you get the KeyEvent before it reaches the window and in my case I definitely get the ENTER key.
But cautious, the event is called twice, once for key down and once for key up.
here a sample code:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
System.out.println(event.getAction() + " " + event.getKeyCode() + " - " + (char) event.getUnicodeChar());
return true;
}

Did you read the documentation?
Key presses in software keyboards will generally NOT trigger this listener, although some may elect to do so in some situations. Do not rely on this to catch software key presses.
Also, your way of capturing keys is very vague. You are not even checking the keyCode sent to you by using:
#Override public boolean onKeyDown(int keyCode, KeyEvent event) { return false; }
You can handle onKey from a View:
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
/* This is a sample for handling the Enter button */
return true;
}
return false;
}
Remember to implement OnKeyListener and to set your listener
viewname.setOnKeyListener(this);

it appears that DPAD keys are acting for the focused items as have been told here:
https://groups.google.com/forum/#!topic/android-developers/HsILBlpsK7I
Although I haven't tried it myself, maybe you can set focus to your view object and attach a key listener function on it.
Update: My colleague had the same problem and this suggestion worked for her. :) She was able to catch the key code from DPAD when the list view is focused.

Related

Android: How to fire a search intent like press ENTER when using `OnQueryTextListener`?

I want to build a instant searchview, which will show the search result immediately in the same layout as soon as a user input anything. So I am googling and decide to use onQueryTextChange to implement that. However I cannot figure out how to "fire" a intent like the normal result which pressing ENTER will lead to, to tell my Activity that there is an event that a user have input something in searchview and you(Activity) should handle it.
Any idea to help?
If I understood you correcty you want to recognize KEY_EVENT (ENTER). I've ended with custom EditText styled like original SearchView, then used OnEditorActionListener:
queryEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
(event != null && event.getKeyCode()==KeyEvent.KEYCODE_ENTER)) {
performSearch();
return true;
}
return false;
}
});
SearchView don't have setOnEditorActionListener method, thats why I'm using simple EditText
note EditorInfo.IME_ACTION_SEARCH option inside "if", you may request "special" keyboard layout with magnify icon instead of Enter key (but remember that not all keyboards are supporting this option and custom keyboard app may show "usual" layout of keyboard)
android:inputType="text|textNoSuggestions"
android:imeOptions="actionSearch"

Android [ENTER]-key action of a multiline edittext in a dialog

I've create a dialog with a multiline edittext-field. The problem is that the [ENTER]-key of the soft keyboard is closing the keyboard instead of creating a new line. With imeOptions, it's possible to configure a lot, but not a newline-command... How can I accomplish this?
Building for a Galaxy Tab 2 with Android 4.0.3.
I found out that setting the raw inputtype of the EditText to multiline is working where the "normal" input type isn't.
final EditText remark = new EditText(MyClass.this);
remark.setRawInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
This did it for me.
You might be able to accomplish this by creating a new TextWatcher. Register this textwatcher to your EditText and implement a breakline when receiveing the return key.
EDIT:
To handle an individual key press, implement onKeyDown() or onKeyUp() as appropriate. Usually, you should use onKeyUp() if you want to be sure that you receive only one event. If the user presses and holds the button, then onKeyDown() is called multiple times.
For example:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
/* This is a sample for handling the Enter button */
return true
default:
return super.onKeyUp(keyCode, event);
}
}
Source:
Android :
http://developer.android.com/training/keyboard-input/commands.html
A list of the KeyEvents:
http://developer.android.com/reference/android/view/KeyEvent.html

How to track the keys pressed in soft keyboard in Android?

How can I track the keys pressed by the user in keyboard in android?
Is this possible or I have to create a keyboard myself?
What could be the possible way ?
Please check Creating An Input Method. This would help you in creating a custom Input Method. You can check the Sample Application in your Android SDK directory. Please follow as per this link.
One way to liste for those is:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Log the Key that was pressed or do whatever you need
// keyCode contains all the possible keyCode presses
}
else if(keyCode == KeyEvent.KEYCODE_1){
//You can insert catches for each particular keypress here
}
return super.onKeyDown(keyCode, event);
}
All the codes are here in this LINK, take a look at them.

android OnEditorActionListener not working with custom soft keyboard

i'm working on a project wich require the use of a custom soft keyboard developed by some one else. The problem is that the setOnEditorActionListener does not work in a specific windows where a fragment is used. Does not work means that the onEditorAction is not fired at all. The problem appens only with the custom keyboard, with the default one every thing is working well. The problem is that the soft keyboard project is very complex because i don't know soft keyboard logics and I need to solve the problem before tomoroow morning. Does anyone have an idea of this behavior? Please help
this is the part where i set the listener, this code is working all around the project but here, even the first listener's line is not reached
((EditText) getView().findViewById(R.seatDetailCommonHeader.txtName)).setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event != null && event.getAction() != KeyEvent.ACTION_DOWN) {
return true;
}
// KeyboardHelper.setKeyboardVisibilty(v, false);
executeCheck();
return true;
}
});
i went into further investigations, i put a breakpoint on every method's first line in the keyboard code (which is the one taken from the sdk samples with just some layout modification) and the same EditText in two different activities fires different methods:
in one case (the working one) this methods are fired when action button is clicked:
LatinKeyboard.isInside
LatinKeyboard.isInside
LatinKeyboard.isInside
LatinKeyboard.isInside
LatinKeyboard.isInside
LatinKeyboard.isInside
LatinKeyboard.isInside
LatinKeyboard.isInside
LatinKeyboard.isInside
SoftKeyboard.onKey
SoftKeyboard.isWordSeparator
SoftKeyboard.sendKey
SoftKeyboard.keyDownUp
SoftKeyboard.keyDownUp
SoftKeyboard.updateShiftKeyState
in the other case (the one that is not working) the same methods are fired, plus these:
SoftKeyboard.onFinishInput
SoftKeyboard.onStartInput
SoftKeyboard.updateShiftKeyState
LatinKeyboard.setImeOptions
SoftKeyboard.onStartInputView
hope someone has some idea of this behavior because i'm really in trouble

Android Key Events for EditText not happening on device

I am new to Android development and having a problem with what SHOULD be a very simple task. I want to receive KeyEvents whenever a user is typing in an EditText field because I want to save their entered values to data structures in the background on each key stroke.
I have mimic'd the code in the Beginner's Dev guide at http://developer.android.com/resources/tutorials/views/hello-formstuff.html#EditText and set up an OnKeyListener. Here is a snippet of my code:
cell.amountEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
System.err.println("onKey for Amount, key="+event.getDisplayLabel());
if (event.getAction() == KeyEvent.ACTION_DOWN) {
return onKeyDownInAmount(finalPosition, (EditText)v, keyCode, event);
} else {
return false;
}
}
});
Behavior on the emulator is spotty at best, some times it will deliver the KeyEvents for the virtual keyboard, sometimes it won't. When I install the app on my device (HTC Hero which has a virtual keyboard only) then NONE of the events fire. I never receive a single KeyEvent.
What am I doing wrong?
Any help is appreciated.
The onKeyListener only receives events from a hardware keyboard. Use TextWatcher instead.

Categories

Resources