Cannot cancel a KeyEvent - android

I have a Bluetooth barcode scanner that acts as a hardware keyboard. I implemented these methods in my Activity (just testing):
#Override
public boolean onKeyUp (int keyCode, KeyEvent event) {
Log.d("debug", "up: "+KeyEvent.keyCodeToString(keyCode));
return true;
}
#Override
public boolean onKeyDown (int keyCode, KeyEvent event) {
Log.d("debug", "down: "+KeyEvent.keyCodeToString(keyCode));
return true;
}
Yet, whatever I return (be it true or false), the keys are always also handled by the default handler, resulting in unwanted behavior.
Update it seems to be missing the KEYCODE_ENTER, which is handled by a default handler. I tried explicitly setting setDefaultKeyMode(DEFAULT_KEYS_DISABLE), but it had no effect.
Am I missing something here?

There are some buttons on the Activity's view that were focused. The focused Button would get my KEYCODE_ENTER and handle it, leaving nothing for my Activity.

Related

Android: catching action for keyboard enter button being pressed

I am trying to respond to pressing the enter button on a standard system on screen soft keybord, in numeric mode. keyboard appears on the screen when myCustomEditText class is given focus.
tried 2 approaches:
overriding onKeyDown():
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
//do sth..
return true;
}
return super.onKeyDown(keyCode, event);
}
setting listener setOnKeyListener:
myCustomEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
onRightButtonClicked();
return true;
}
return false;
}
});
(also tried onKeyDown() in myCustomEditText as well, but with no effect)
problem is:
these aproaches above works for all the keys being pressed but for the enter key - the methods are not even being fired at all. (it's not a problem of bad KeyEvent.KEYCODE_...)
my question is:
why these methods are not being fired for enter key, but are for other keys, and how to listen to enter key being pressed properly?
You respond to ENTER differently than normal keys, try something like this:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch (actionId) {
case EditorInfo.IME_ACTION_DONE:
...
return true;
default:
return false;
}
}
});
You can specify which action is performed by ENTER by specifiying android:imeOptions in XML or by calling editText.setImeOptions(...) with one of the constants in EditorInfo. imeOptions also changes how the ENTER key looks! For example if you set imeOptions to actionSearch then the ENTER key will look like a search button.
You can find a list of all possible values for imeOptions along with a description here.

onKeyUp doesn't work for KEYCODE_DPAD_CENTER

I have 2 activities, where I overrided the onKeyUp event:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.d(TAG, "Key up: " + keyCode);
return super.onKeyUp(keyCode, event);
}
The first is the main activity of the application, the other one is secondary. The first works fine, but the second captures events for Left-Right-Up-Down but not for Center click. Why?
Since you have a Gallery in your layout, take a look at: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/Gallery.java#L1216 . It might be possible that the Gallery steals your DPAD_CENTER event (and also ENTER).
I suggest you to use Activity.dispatchKeyEvent(android.view.KeyEvent) to stop the event before it even goes to that Gallery.
Try using it like this (in your Activity):
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
// Do your stuff here.
return true; // Consume the event (or not, your call)
}
return super.dispatchKeyEvent(event);
}

How to keep DropDownList of AutoCompleteTextView opened after pressing the Back key?

i am using AutoCompleteTextView in my Activity and i need it's DropDownList to be shown all the time (it's the only View in Window), even after Back key press. I need to dismiss soft keyboard instead.
I tried to override Activity's onBackPressed method, but it's not used at all, so BackPressed event is being handled somewhere "higher". So i tried to find out where, but AutoCompleteTextView has no onBackPressed method defined.
Any advices?
You can create your custom AutoCompleteTextView and Override the method onKeyPreIme(int keyCode, KeyEvent event)
I also realized that this method is called 2 times, I'm running my code only in the second time. Here is the example:
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) {
//add your code here
return true;
}
return super.onKeyPreIme(keyCode, event);
}
You may try this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Your back key press logic
}
return true;
}
Remember to return true to prevent this event from being propagated further, or false to indicate that you have not handled this event and it should continue to be propagated.

How to use menu and onKeyDown() on the same activity

I am developing an Android app. I have written my own code for back key event.
For that i am catching events using this method
public boolean onKeyDown(int keyCode, KeyEvent event) .....
I also want to use Menu options I have used Inflator for that.
But when I click on menu button, that event caught by my onKeyDown method.
I am not expecting that, I expect to execute following methods,
public boolean onCreateOptionsMenu(Menu menu){ // creating menu using MenuInflator }
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case :
return true;
default:
}
}
How could I use both the things on the same activity?? Any Idea ???
According to the documentation of onKeyDown():
it is normal that it receives the 'menu button pressed' event, as any KeyEvent
but if you return false, this event will continue to be propagated and will therefore lead onOptionsItemSelected() to be called
So you should code your onKeyDown() in a way that lets the KeyEvent to propagate if you do not handle it on your own.
In the following example, we have two keys that are interesting to us: one we fully handle on our own, one we just track, and we ignore all the rest:
protected boolean onKeyDown(int keyCode, KeyEvent event) {
boolean handled = false; // By default we let the event propagation turned on
if (keyCode == A_KEY_YOU_WOULD_LIKE_TO_HANDLE_BY_YOURSELF) {
doSomething();
handled = true; // Stops the event propagation
}
else if (keyCode == A_KEY_YOU_WOULD_LIKE_TO_TRACK_WITHOUT_PREVENTING_THE_SYSTEM_FROM_HANDLING_IT) {
doSomethingElse();
}
// else, you neither handle or track that key
return handled;
}

Android: onKeyListener simulating two instances of a pressed key [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
public boolean onKey() called twice?
Display.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
solveExpression();
return true;
}
return false;
}
});
I'm trying to solve the expression contained within the Display(EditText), by pressing the enter button on the keyboard, yet it always interprets it as though I pressed the button twice. Does anyone know why this happens?
Try...
Display.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
// Check for ACTION_DOWN only...
if (KeyEvent.ACTION_DOWN == event.getAction()) {
solveExpression();
return true;
}
}
}
});
The 'action' can be ACTION_DOWN, ACTION_UP or ACTION_MULTIPLE (the last being for when a key is pressed and held). onKey() will be called for any/all of those actions.
As the other answer mentions, it's triggering twice because it's once for down and once for up.
if (event.getAction()!=KeyEvent.ACTION_DOWN) // we catch only key down events
return true;
Thus you stop listening other keyevents as onClick.
If you want nobody else further in chain to get the event for another piece of work, you should set
return false;
not an android guy either but the fact that it registers twice makes me think that OnKey encompasses a onKeyDown and onKeyUp. Would listening to onKeyUp work for you as well?

Categories

Resources