I have attached a physical keyboard to an Android tablet using USB. How can I call a function or perform some action in an Android app on pressing a paticular key, for example when pressing the zero key?
How could I implement the below code (for the zero key)?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_A:
{
// your Action code
return true;
}
}
return super.onKeyDown(keyCode, event);
}
It was such a silly mistake i didnt import Keyevent ..
Related
I have a custom device with a custom button and I need to handle an hardware Button Events/Intents:
every time I press the button it generates a PTT Press Action and I need to open my custom application, is there a way to do this?
If by custom device, you mean custom AOSP. Then make sure, it's button bound to events, this should be done with low level device driver configuration. And it's complicated work. Next, make sure you receive your click in next events.
Try to either use dispatch key event on Activity work:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_UP){
enter();
return true;
}}
return super.dispatchKeyEvent(event);
};
On on key event with Android View which is in focus.
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;
}
I have this event handler in my activity:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
//do something
return true;
case KeyEvent.KEYCODE_MEDIA_REWIND:
//do something
return true;
default:
return super.onKeyUp(keyCode, event);
}
}
While debugging in Android TV device emulator, I can see KEYCODE_DPAD_LEFT and KEYCODE_MEDIA_PLAY_PAUSE when I press a button in directional pad extended control.
But when I press "fast forward" or "rewind" media keys, the key up event is triggered, but the key code is "unrecognized".
KeyEvent { action=ACTION_UP, keyCode=KEYCODE_UNKNOWN, scanCode=208...
Is this a Google bug or am I doing something wrong here?
It seems that the TV Emulator actually does not provide the correct keycode here, which seems to be a bug.
You can simulate this via the command line:
adb shell input dpad keyevent 90
This will trigger the KEYCODE_MEDIA_FAST_FORWARD button.
This is my code when I press the volume up button to next / prev songs :
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
prevSongs();
return true;
} else if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
nextSongs();
return true;
} else return super.onKeyDown(keyCode, event);
}
But when the application exits (with the home button to return to the home screen) and I press the volume up button again, it does not work.
How does it work whether it's in or out of the app?
Thank !
You need to create a service to handle your playing media. When you exit the app, you can't listen to volume events, but in service you can do it. Also, listening to volume keys to change songs is a bad idea. You should use a notification with actions (buttons).
I'm doing an Android app and I would know if this was possible:
When the user press the return button, it does the same action as the home button.
Following the question I linked: Override back button to act like home button:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
Should be the solution.
This overrides the backbutton, and puts the task to the back. Which emulates hiding like the home button. Read the other question for more details, and solutions.
I want to stop media sound when the user press hold key or power key
I do this
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENDCALL || keyCode == KeyEvent.KEYCODE_POWER){
pauseSound();
}
return super.onKeyDown(keyCode, event);
}
But this override function didn't invoke when Power or end call is pressed
Any idea?
Thanks it solved, Ovveride EndCall & Power is forbidden for security reasons, I overrided "OnPause" (of activity) and it works good, Thanks again