I have an onKeyDown event which wont recognise the first key press (wont even enter the event, i have tested by producing a 'toast' output). On the second key press and after, it works perfectly. If I click on another element on the screen and try the key press again, it yet again needs another key press to get it going. Here is the code:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
scorered.performClick();
return true;
case KeyEvent.KEYCODE_1:
red_m1.performClick();
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
scoreblue.performClick();
return true;
case KeyEvent.KEYCODE_2:
blue_m1.performClick();
return true;
case KeyEvent.KEYCODE_BACK:
finish();
return true;
}
return true;
}
I have been stumped for hours so any help is much appreciated!
I'm sure, there are good reasons for such behaviour, but do not think, that removing focus is a good solution. My workaround is to fire a keydown event, that "activate" regular onKeyDown functionality. Here is snippet:
new Thread(new Runnable() {
#Override
public void run() {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);
}
}).start();
To fix this, a work-around is to remove the view focus before pressing any DPAD key. It works in my case. I am having exactly the same issue: When a view of Android Activity is on focus, the very first DPAD key event, that is, the KeyDown event, is ignored: None of these methods are called: onUserInteraction(), dispatchKeyEvent(), onKeyDown(). However, subsequent DPAD key events - KeyUp, KeyDown, KeyUp, ..., can be captured.
Note that this issue does not occur with soft keys (Home, Previous, Recents) nor with hard button keys (BUTTON_A, BUTTON_B, BUTTON_X, BUTTON_Y).
Related
I was wondering how does the home button or volume up/down button works in Android? Does pressing this button generates a hardware interrupt?
I would like to execute a piece of code with higher privilege (in kernel) by pressing this home/volume key. Is it possible? Any pointers?
if pressing these button generates a hardware interrupt, I think I have to modify the interrupt handler to execute the code I want to execute. Is this correct?
Thanks in advance!
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU: // Menu button
doSomething();
return true;
case KeyEvent.KEYCODE_HOME: // Home button
doSomething();
return true;
case KeyEvent.KEYCODE_VOLUME_UP: // Volume Up key
doSomething();
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN: // Volume Down key
doSomething();
return true;
}
return super.onKeyDown(keycode, e);
}
You can find more keys here.
I'm making a game with OpenGL Es 1.1 and i want to implement back button or menu button functionality to my game.(I mean hardware buttons).I have some subClasses so what i have to do when i want to handle hardware button press from subClasses?
You need to implement an onKeyDown listener and check to see what key is pressed.
Sample:
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
handleMenuButton();
return true;
case KeyEvent.KEYCODE_BACK:
handleBackButton();
return true;
}
return super.onKeyDown(keycode, e);
}
Also note that for the back and menu buttons to fire reliably, you need to set setFocusableInTouchMode to true. See the devguide here (scroll down to "Touch Mode").
I am trying to capture the key-press for the shift or alt keys and it seems to work just fine with the physical keyboard. Unfortunately when I test the same code with the onScreen keyboard it doesn't respond at all (I've tried a 2.1, 4.03 emulator and 2.3 physical device).
This seems to be a platform issue that probably has no workaround, but as a last ditch effort I wanted to post the question here to see if anyone has found one (I am willing to entertain any ideas).
Thanks!
Edit to clarify my point: I tried putting a breakpoint on the " if (event.getAction() == KeyEvent.ACTION_DOWN) " line, and it will always stop if I press the shift of the physical keyboard, but never when I press the shift of the onScreenKeyboard. So, the problem is that onKey is not being executed for all keypresses of the onScreenKeyboard.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEditText1 = (EditText) findViewById(R.id.editText1);
mEditText1.setOnKeyListener( new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
Toast.makeText(IMETestActivity.this, "Enter was consumed",
Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.META_SHIFT_LEFT_ON:
case KeyEvent.META_SHIFT_RIGHT_ON:
Toast.makeText(IMETestActivity.this, "Meta Shift was consumed",
Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_SHIFT_LEFT:
case KeyEvent.KEYCODE_SHIFT_RIGHT:
Toast.makeText(IMETestActivity.this, "Shift was consumed",
Toast.LENGTH_SHORT).show();
return true;
default:
break;
}
}
return false;
}
});
}
Try logging the keycode whenever a key is pressed to make sure that the event is registering. If it is, just add that code to your switch statement.
For example, something like System.out.println(keyCode) at the beginning of your onKeyListener. You can view the output using LogCat.
I am new to android development, making a simple game and using onKeyDown(.....) function but it works only for 1 key at a time, how to handle 2 keys at a time means can i move right or left with continous firing.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode){
case KeyEvent.KEYCODE_DPAD_RIGHT:
bottle_movx+=2;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
bottle_movx-=2;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
fire=bottle_movx;
firechk=true;
break;
}
invalidate();
return super.onKeyDown(keyCode, event);
}
EDIT: Perhaps this could be of some help?
How do I handle simultaneous key presses in Java?
Quote:
One way would be to keep track yourself of what keys are currently down.
When you get a keyPressed event, add the new key to the list; when you get a keyReleased event, remove the key from the list.
Then in your game loop, you can do actions based on what's in the list of keys.
EDIT 2: I also found this link which could be useful:
How do I handle multiple key presses in a Java Applet?
Please also check the action event( event.getAction() ). This would return the constant defined in the KeyEvent class like ACTION_DOWN, ACTION_UP etc. Please check for either up or down action.
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?