Game Development Reference
In-Depth Information
Figure 6-4. Wolf 3D main menu and navigation options dialog
Handling Key and Touch Events
Listing 6-4 shows the way the game handles key and touch events within the main activity.
When the user presses a key, onKeyDown first checks if the menu key has been
pressed. It ignores the event if so. Otherwise, it translates the Android key code to
a PC scan code with ScanCodes.keySymToScancode(keyCode) and sends it to the
DSO using the Natives class key press.
When the key is released, onKeyUp will be called performing similar steps as before.
Menu keys are ignored, and the key code gets translated to a scan code and sent to
the DSO as a key release event.
When the screen is touched, onTouchEvent will be called. This method will send a
CONTROL scan code to the DSO, which indicates the gun should be fired.
Listing 6-4. Event Handlers Within the Main Activity
/**
* Key down
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Ignore MENU
if (keyCode == KeyEvent.KEYCODE_MENU)
return false;
try {
int sym = ScanCodes.keySymToScancode(keyCode);
Natives.keyPress(sym);
} catch (UnsatisfiedLinkError e) {
System.err.println(e.toString());
Search WWH ::




Custom Search