arrow key code for smartphone?

  • Replies:5
Stephen Kong
  • Forum posts: 17

Feb 22, 2012, 12:28:49 AM via Website

Does Android 3.2 smartphones all support arrow/direction keys? If so, what are the keyCode for the four keys?

does KeyEvent class already defined these keys?

— modified on Feb 22, 2012, 12:32:37 AM

Reply
Eric McBride
  • Forum posts: 1,790

Feb 22, 2012, 1:34:40 PM via Website

Im not sure I understand your question. Could you be a bit more specific?

Reply
Stephen Kong
  • Forum posts: 17

Feb 22, 2012, 3:13:27 PM via Website

I am writing a game where a spaceship will be able to move up/down/right/left. What would be the best way to navigate the spaceship? If keyboard is used, what keys would be best for this purpose? What are the identifiers in KeyEvent class?

Reply
Eric McBride
  • Forum posts: 1,790

Feb 22, 2012, 5:09:33 PM via Website

Well, for phones with a physical keyboard, the "A" key is typically used for left, the "D" key for right, the "S" key for back, and the "W" key for forward.

For phones just with a touchscreen, you could either use tilt controls or an on screen gamepad.

— modified on Feb 22, 2012, 5:09:46 PM

Reply
Jeremiah
  • Forum posts: 775

Feb 23, 2012, 4:23:12 AM via Website

You can get the key pressed events from your activities view. Override the KeyDown and KeyUp events like this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {

if (keyCode==KeyEvent.KEYCODE_DPAD_LEFT) { //DO CODE FOR LEFT KEY PRESSED

}
@Override
public boolean onKeyUp(int keyCode, KeyEvent msg) {
}


The KeyEvent class has constants for all the buttons keycodes: http://developer.android.com/reference/android/view/KeyEvent.html

For the arrow buttons you would use KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN, AND KeyEvent.KEYCODE_DPAD_CENTER

Not all phones have a directional key pad. For these phones you want to add a way to navigate the ship. You could let them use the A, D, S, W like Eric said, though some phones don't have a keyboard at all so you need to add touch or tilt controls as well. The best way to do it, is let the player choose which kind of controls he wants. You can even let them assign their own keys to the actions. You could have an options screen that asks them to choose/press which key to move the ship up. Then when they press a key save the keyCode they pressed as an integer, then when they are playing check to see if the keyCodes they are pressing matches one of the integers you saved.

Like this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
//Psuedo coding here, you would integrate this into your own options settings.

if (state==CHOOSELEFTKEY) {
userSettingsLeftKey=keyCode;
}


if (state==playingGame) {
if (keyCode==userSettingsLeftKey) { //DO CODE FOR LEFT KEY PRESSED

}
}

— modified on Feb 23, 2012, 4:31:49 AM

Reply
Eric McBride
  • Forum posts: 1,790

Feb 23, 2012, 3:17:30 PM via Website

Thanks Jeremiah. Perfect explanation!

Reply