I have provided you with a sample of coding that is useful for key event handling. Just have a look on this coding :
Code:
public class KeyEventTrial ... implements KeyListener ... {
typingArea = new JTextField(25);
typingArea.addKeyListener(this);
...
public void keyTyped(KeyEvent ke) {
displayInfo(e, "KEY TYPED: ");
}
public void keyPressed(KeyEvent ke) {
displayInfo(e, "KEY PRESSED: ");
}
public void keyReleased(KeyEvent ke) {
displayInfo(e, "KEY RELEASED: ");
}
...
private void displayInfo(KeyEvent ke, String keyStatus){
int id = ke.getID();
String keyString;
if (id == KeyEvent.KEY_TYPED) {
char c = ke.getKeyChar();
keyString = "key character = '" + c + "'";
} else {
int keyCode = ke.getKeyCode();
keyString = "key code = " + keyCode
+ " ("
+ KeyEvent.getKeyText(keyCode)
+ ")";
}
int modifiersEx = ke.getModifiersEx();
String modString = "extended modifiers = " + modifiersEx;
String tmpString = KeyEvent.getModifiersExText(modifiersEx);
if (tmpString.length() > 0) {
modString += " (" + tmpString + ")";
} else {
modString += " (no extended modifiers)";
}
String actionString = "action key? ";
if (e.isActionKey()) {
actionString += "YES";
} else {
actionString += "NO";
}
String locationString = "key location: ";
int location = e.getKeyLocation();
if (location == KeyEvent.KEY_LOCATION_STANDARD) {
locationString += "standard";
} else if (location == KeyEvent.KEY_LOCATION_LEFT) {
locationString += "left";
} else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
locationString += "right";
} else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
locationString += "numpad";
} else { locationString += "unknown";
}
}
}
Bookmarks