I'm using a JTextfield wich should react to the VK_DOWN key in a keytyped event.
KeyPressed and KeyReleased work perfect, but KeyTyped i a mess: If I press a alphanumeric character (A, B, C, ..., 1, 2, 3 ...) the result of getKeyCode() is allways 0 Even better: There is no KeyTyped event generated at all by I press any non-alphanumeric key (ex: no event for ALT, CTRL, VK_DOWN, F1 ...)
I don't know if a standard textfield has the same errors.
I'm using Java 1.5.0_02 All this worked once (I think in 1.4).
unsure if its the best method, but it worked, I used the following code to determine if the 'Ctrl', 'Shift' or 'Alt' key were pressed
// Shift, Ctrl or Alt ??? if(e.getKeyText(e.getKeyCode()).equalsIgnoreCase("Alt") || e.getKeyText(e.getKeyCode()).equalsIgnoreCase("Shift") ||e.getKeyText(e.getKeyCode()).equalsIgnoreCase("Ctrl")) { return; } //ALSO // used the following code to determine whether // left, right, up and down arrows, Function keys, Home, End etc. keys were pressed if( e.isActionKey() ) { return; }
The keyTyped event is high level event and is generated only for the Unicode characters. So if you want to work with Unicode characters then you should write the code in this function. You can also get the status of ALT, CRTL and SHIFT key in this. But no keycode.
keyPressed and keyReleased events are low level events and are generated for the all the keys present on the keyboard. So no matter which key you press on keyboard, keyPressed and keyReleased event will be generated(except for TAB). If you are working with funtion keys then use these function.