I'm trying to code simple wasd controls in android studio. I was trying to use onKeyDown and onKeyUp to move my character while the key is pressed by having onKeyDown set a boolean for my movement to true, and then having onKeyUp set it to false. Right now though when I press and hold a key they both just get called over and over, and nothing happens when I release the key. I know onKeyDown is supposed to behave that way. But why is onKeyUp doing the same thing? Here's an example of my simplified onKeyUp function I'm using for testing:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.d(TAG, "onKeyUp: keycode=" + keyCode + ", event=" + event);
switch (keyCode) {
case KeyEvent.KEYCODE_A:
Log.d(TAG, "up key a");
break;
case KeyEvent.KEYCODE_D:
Log.d(TAG, "up key d");
break;
default:
return super.onKeyUp(keyCode, event);
}
return true;
}
How do I catch when the key is actually released?
Related
I'm trying to capture keyboard input in Android through the AVD Emulator all the keys work except for the Function Keys. When I press the Function keys nothing happens except opening the help menu with F1. The onKeyDown event doesn't even fire. How do I enable them and how do I prevent the help menu from appearing when pressing F1 if I can enable the Function keys?
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.e("Key", String.valueOf(event.getKeyCode()));
switch (keyCode) {
case KeyEvent.KEYCODE_F1:
Log.e("Key", "F1 Pressed.");
case KeyEvent.KEYCODE_F2:
Log.e("Key", "F2 Pressed");
default:
Log.e("Key", String.valueOf(event.getKeyCode()));
return super.onKeyUp(keyCode, event);
}
}
Thank you!
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.
How can i use Android Wear Wrist Gestures in Wear Apps? Is it possible to detect them for e.g. scrolling on flicking the wrist?
If you register the callback onKeyDown() in your activity, you should be notified of the following events: KEYCODE_NAVIGATE_NEXT,
KEYCODE_NAVIGATE_PREVIOUS, KEYCODE_NAVIGATE_IN and KEYCODE_NAVIGATE_OUT. These new gestures are mapped to these events. So here is a quick override:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(TAG, "onKeyDown Keycode: " + keyCode);
switch (keyCode) {
case KeyEvent.KEYCODE_NAVIGATE_NEXT:
// Do something that advances user view to the next item in an ordered list
return true;
case KeyEvent.KEYCODE_NAVIGATE_PREVIOUS:
// Do something that advances user view to the previous item in an ordered list
return true;
case KeyEvent.KEYCODE_NAVIGATE_IN:
// Do something that activates the item that currently has focus or expands to
// the next level of a navigation hierarchy
return true;
case KeyEvent.KEYCODE_NAVIGATE_OUT:
// Do something that backs out one level of a navigation hierarchy or collapses
// the item that currently has focus.
return true;
default:
// If you did not handle, then let it be handled by the next possible element as
// deemed by Activity.
return super.onKeyDown(keyCode, event);
}
}
I believe you will find the HandsFree Wear application interesting (I'm the creator), with witch you can (almost) fully use your android smartwatch through wrist gestures.
https://github.com/jsalatas/HandsFreeWear
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
I'm trying to listen for the search key, I've found two solutions on stackoverflow but neither works with the search button for some reason.
#Override
public boolean onSearchRequested() {
System.out.println("KEYDOWN");
return true;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("KEYDOWN");
return true;
}
As you can see the first one just the search intent and the second option should catch all keydowns regardless the key, onKeyDown is working correctly for all keys (back, menu and volume keys in my phone) in my Nexus S but simply doesn't catch the search key. People seem to be pretty sure any of this options should work there is even an approved where onSearchRequested is suggested but I have tried in two phones and in neither of them works.
How can I catch and disable the search key on android?
Try this :
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Toast.makeText(getApplicationContext(), "Fired", Toast.LENGTH_LONG).show();
if (keyCode == KeyEvent.KEYCODE_SEARCH) {
Toast.makeText(getApplicationContext(), "Search Key Fired", Toast.LENGTH_LONG).show();
return true;
}
return super.onKeyDown(keyCode, event);
}
OR :
#Override
public boolean onSearchRequested() {
Toast.makeText(getApplicationContext(), "onSearchRequested", Toast.LENGTH_LONG).show();
return false;
}
NOTE :
If your device does enjoy the latest and greatest OS, go ahead and tap
that Google Search bar, and you’ll be greeted by the “Get Google Now!”
screen (Figure A). At this point, you can choose to enable Google Now
or dismiss the feature for later. You can also tap “Learn more” to
find out what Google Now offers.
Reference :
Google Now on Android Jelly Bean is more than just a search feature
Thanks