Game Development Reference
In-Depth Information
Key and Touch Event Handlers
Key and touch handlers can be overridden to process key and touch events. Doom handles key and
touch events as follows (see Listing 7-5):
The Android
keyCode
is first translated to an ASCII key symbol by calling
int sym =
DoomTools.keyCodeToKeySym(keyCode)
.
•
•
The ASCII symbol is the sent to the DSO through the native interface class
Natives.keyEvent(EVENT_TYPE, SYMBOL)
, where the event type must be either
Natives.EV_KEYUP
or
Natives.EV_KEYDOWN
. Note that any errors in the native side
(such as a missing symbol or invalid signature) will throw an
UnsatisfiedLinkError
.
Listing 7-5.
Key and Touch Handlers
public boolean onKeyUp(int keyCode, KeyEvent event) {
//
if (keyCode == KeyEvent.KEYCODE_MENU) {
return false;
}
int sym = DoomTools.keyCodeToKeySym(keyCode);
try {
Natives.keyEvent(Natives.EV_KEYUP, sym);
} catch (UnsatisfiedLinkError e) {
// Should not happen
Log.e(TAG, e.toString());
}
return false;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Ignore menu key
if (keyCode == KeyEvent.KEYCODE_MENU) {
return false;
}
int sym = DoomTools.keyCodeToKeySym(keyCode);
try {
Natives.keyEvent(Natives.EV_KEYDOWN, sym);
}
catch (UnsatisfiedLinkError e) {
// Should not happen
Log.e(TAG, e.toString());
}
return false;
}