Java Reference
In-Depth Information
Example 15−4: EventTester.java (continued)
showLine("Lost Focus"); return true;
}
// Handle key down and key up events
// This gets more confusing because there are two types of key events
public boolean keyDown(Event e, int key) {
int flags = e.modifiers;
if (e.id == Event.KEY_PRESS) // a regular key
showLine("Key Down: " + mods(flags) + key_name(e));
else if (e.id == Event.KEY_ACTION) // a function key
showLine("Function Key Down: " + mods(flags) +
function_key_name(key));
return true;
}
public boolean keyUp(Event e, int key) {
int flags = e.modifiers;
if (e.id == Event.KEY_RELEASE) // a regular key
showLine("Key Up: " + mods(flags) + key_name(e));
else if (e.id == Event.KEY_ACTION_RELEASE) // a function key
showLine("Function Key Up: " + mods(flags) +
function_key_name(key));
return true;
}
// The remaining methods help us sort out the various modifiers and keys
// Return the current list of modifier keys
private String mods(int flags) {
String s = "[ ";
if (flags == 0) return "";
if ((flags & Event.SHIFT_MASK) != 0) s += "Shift ";
if ((flags & Event.CTRL_MASK) != 0) s += "Control ";
if ((flags & Event.META_MASK) != 0) s += "Meta ";
if ((flags & Event.ALT_MASK) != 0) s += "Alt ";
s+="]";
return s;
}
// Return the name of a regular (non-function) key.
private String key_name(Event e) {
char c = (char) e.key;
if (e.controlDown()) {
// If CTRL flag is set, handle control chars.
if (c < ' ') {
c += '@';
return "ˆ" + c;
}
}
else { // If CTRL flag is not set, then certain ASCII
switch (c) { // control characters have special meaning.
case '\n': return "Return";
case '\t': return "Tab";
case 'ˆ[': return "Escape";
case 'ˆH': return "Backspace";
}
}
// Handle the remaining possibilities.
if (c == 'ˆ?') return "Delete";
else if (c == ' ') return "Space";
Search WWH ::




Custom Search