libgdx in Android: how to handle escape key? - java

In Android. I implement an InputProcessor and:
#override public boolean keyDown(int keycode)
When I press escape, no key is checked and the app exits. How do I check Escape key?
ps: in PC/MAC ,I can check Escape directly, and it works.

This works for me:
public boolean keyDown(int keycode){
switch (keycode){
case Keys.ESCAPE:
Gdx.app.log("Monles", "ESC pressed");
break;
...
Also, my app doesn't quit when I press ESCAPE, but if yours do, this will probably fix it:
Gdx.input.setCatchBackKey(true);

First create an InputProcessor to handle the back key actions and then add it to a multiplexer so that other components still receive similar events.
InputProcessor backProcessor = new InputAdapter() {
#Override
public boolean keyDown(int keycode) {
if ((keycode == Keys.ESCAPE) || (keycode == Keys.BACK) )
// Maybe perform other operations before exiting
Gdx.app.exit();
return false;
}
};
InputMultiplexer multiplexer = new InputMultiplexer(mStage,
backProcessor);
Gdx.input.setInputProcessor(multiplexer);
And enable catching the back key.
Gdx.input.setCatchBackKey(true);
That's all there is. Good luck!

Related

keyCode == KeyEvent.KEYCODE_BUTTON_Y freezing the focus and not able to navigate after that

For Nvidia joystick trying to associate this event to go to search button on the grid view, it is working but it is getting frozen and nothing is happening after that. Suggest me something
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.i(TAG, " :: keyCode ::"+keyCode);
if (keyCode == KeyEvent.KEYCODE_BUTTON_Y) {
searchTop.performClick();
return true;
}
return super.onKeyUp(keyCode,event);
}
Try to call the super.onKeyUp(keyCode,event) anyway, even after your event handling.

Listening to key press events blocks the keys from working

I'm new to android and I'm making an app that will listen for a consecutive volume up and volume down keypress and vibrate a pattern.
I've tried using dispatchKeyEvent() and it detects volume up and down fine but it blocks the user from changing the volume.
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
System.out.println("UP"); // I know i can use Log but this is quicker to type
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
System.out.println("DOWN");
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
Additionally I'd like to be able to do this when the screen is off (and app in background) but aparently services can't interact with key events.
I've found very conflicting answers all over the place; an app I used before played a sound when it was in the background, screen turned off and the power button was pressed 3 times in short succession, however many questions here have answers akin to "not possible".
I wouldn't mind using power button instead of volume keys (infact both would be nice) but that seems harder to implement.
TLDR:
How to stop listeners blocking the keys they're listening.
2. How to run this in the background.
Any help would be greatly appreciated!
Edit: regarding no2 I found a way to do this with power button by registering a broadcast receiver in a service and listening to screen off and screen on intents.
overriding the onKeyDown() of Activity is easier; because it's always KeyEvent.ACTION_DOWN:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP: {
System.out.println("UP");
}
case KeyEvent.KEYCODE_VOLUME_DOWN: {
System.out.println("DOWN");
}
}
return super.onKeyDown(keyCode, event);
}
How to stop listeners blocking the keys they're listening.
As you could see from android docs, if you don't need to block your keys, just call super.dispatchKeyEvent(event); otherwise if you return true; it means that you consumed event, nobody will know about event.
How to run this in the background.
I think, there is impossible. Android has service, which could do background job, but this not way to handle system button clicks. Maybe this topic will help you.

Change volume key function in android app

I have source code for SmartMouse android app.
I want to alter the function of volume keys with the onscreen buttons.
I have basic knowledge for C programming but don't know java.
What part should I search for in the code?
This might be a lame question but I badly need this.
You have to capture the event as mentionned here : Android - Volume Buttons used in my application
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
//TODO
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
dispatchKeyEvent is not only called for volume keys, it will catch all the key event so you have to :
Get the event code
Check if it's what you are seeking for
Do what you want according to the event :)
The key is dispatchKeyEvent is called before any other method by the system, so you can intercept the event

Hold down volume button on android?

I'm trying to make an app where if you hold down the volume down button, an action occurs. How would I go abouts doing this? I'm a noob for java, so an example would be excellent.
Override onKeyLongPress in your Activity
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
// to your stuff here
return true;
}
return super.onKeyLongPress(keyCode, event);
}
how to capture long press volume down key in android?

Pressing the green key in blackberry

I want to know when the user presses the green button in the device to initiate a phonecall from the app directly. What event is fired when the green key is pressed?
Thanks
You need to implement KeyListener
import net.rim.device.api.system.KeyListener;
override the function
public boolean keyDown(int keycode, int time)
and inside it catch the event of pressing buttons.
How do you check which button was pressed?
if (Keypad.KEY_SEND == Keypad.key(keycode)) {//your code}
find the API DOC here : http://www.blackberry.com/developers/docs/4.0.2api/net/rim/device/api/ui/Keypad.html
I found the answer to be something similar to this.
Override keyDown:
public boolean keyDown(int keycode, int time)
{
if (keycode == Keypad.SEND)
{
//handle your event
return true;
}
return super.keyDown(keycode, time);
}

Categories

Resources