On a Samsung Tablet, we have the following keyboard:
When a clic occurred on the right bottom arrows, in the view pager, fragment is changed. I want to intercept this event or remove the arrows from the keyboard. Is it possible ?
I tried to intercept keyevent with onKeyUp and onKeyDown methods, but it's not working:
if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
return true;
}
My problem is the same as here
Try to use dispatchKeyEvent(KeyEvent event):
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.i("key pressed ", String.valueOf(event.getKeyCode()));
if(event.getKeyCode() == KeyEvent.KEY_A /*select required keycode*/ ){
// perform task
}
return super.dispatchKeyEvent(event);
}
Related
I'm wondering which KeyEvent action is called when a user presses the little upside-down triangle in nexus phone when soft keyboard is open.
In normal mode Nexus looks like this and the normal code works fine:
Nexus without keyboard
But when keyboard pops up it looks like this and the code won't work:
Nexus with keyboard
For android API up to 5:
#Override
public void onBackPressed() {
// your code.
}
For android before API 5 you must use this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
Please refer to How to handle back button in activity
EDIT:
This methods works only if the keyboard is hidden..
according with this answer: Detect back key press - When keyboard is open
The best action to be implement is dispatchKeyEventPreIme.
An example is:
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
Log.d(TAG, "dispatchKeyEventPreIme(" + event + ")");
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
KeyEvent.DispatcherState state = getKeyDispatcherState();
if (state != null) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getRepeatCount() == 0) {
state.startTracking(event, this);
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP
&& !event.isCanceled() && state.isTracking(event)) {
mActivity.onBackPressed();
return true;
}
}
}
return super.dispatchKeyEventPreIme(event);
}
Where mActivity is your activity class (this).
While doing digits programming, i am using following method :
Digits.authenticate(authCallback,R.style.CustomDigitsTheme1);
which directs me to digits authentication screen(without showing my xml design file).
Now, when i press back button, it shows me my xml with digits auth button as below.
which i do not want.I tried conventional ways of disabling back buttons but they did not work. Is there any way i can disable back button on authentication???
You can override when the keyboard disappears using this method:
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK &&
event.getAction() == KeyEvent.ACTION_UP) {
// Do your thing here
return false;
}
return super.dispatchKeyEvent(event);
}
This may helps you.
Try Using these
#Override
public void onBackPressed() {
// your code.
}
and for older then API 5 use this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
My application works with remote Bluetooth keyboard. I have defined some predefined custom implementation on arrow keys and enter key. I want to perform only those actions whenever user press arrow key or Enter Key in my application.
But sometimes it is still showing default behavior of keys.
My Current Code is as follows
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
// my custom work
return true;
}else{
return super.onKeyUp(keyCode, event);
}
}
any idea, how I can restrict default behavior of arrow keys and enter key?
Thanks
Use below code:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP || keyCode == KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode == KEYCODE_DPAD_RIGHT || keyCode == KEYCODE_ENTER) {
// my custom work
return false;
}else{
return super.onKeyUp(keyCode, event);
}
}
I need a listener to identify the keypress in the soft keyboard/on screen keyboard.
I tried with addtextchangelistener textwatcher but this one give the good result but it shows the change also when some text is pasted into it.
I need to identify only the key press by the user.
Is there any possible way to detect the key press.
see this keyevent and use following code to identify which key is pressed by Users.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// Do Code here
}
else if(keyCode == KeyEvent.KEYCODE_0)
{
}
else if(keyCode == KeyEvent.KEYCODE_1)
{
}
return super.onKeyDown(keyCode, event); }
When handling keyboard events with the KeyEvent class and related
APIs, you should expect that such keyboard events come only from a
hardware keyboard. You should never rely on receiving key events for
any key on a soft input method (an on-screen keyboard).
see: Handling Keyboard Actions
See this if can help you.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 1) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Do Code here
}
return super.onKeyDown(keyCode, event);
}
For my android app I have multiple edittexts on the main screen. If i have the first edittext in focus the menu/back buttons operate fine, if i have any of the other edittexts in focus than neither of them work at all. I'm not doing anything special regarding the menu/back buttons relative to that edittext, i'm not sure what the cause is? Has anyone run into a similar issue and/or know of the cause/solution?
Thanks!
I was have the same problem and I found solution for I was have OnKeyListener that return true; when I change it to return false; the problem was fixed
my_editText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// you can use the next line to ensure back button & menu button will return false
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK || event.getKeyCode() == KeyEvent.KEYCODE_MENU) return false;
// any other key you don't want to call other listener for it
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER&& event.getAction() == KeyEvent.ACTION_UP) {
return true;
}
return false;
}
});
OnKeyListener.onKey(android.view.View, int, android.view.KeyEvent)
easy fix for me, I had removed the only keyboard app.
just install a keyboard app and the buttons worked again