I have a question about a KeyListener
.
When I get the KeyEvent
and do a getKeyChar()
I'm tyring to compare to and *
asterisk and I was going to use one of the KeyEvent.VK_
defines which works for a lot of the keys.
But for this particular key and some others the values don't match up.
The * getKeyChar()
will return 0x2a
and the getKeyCode()
returns 0x38
. The define for 0x38
is VK_8
not VK_ASTERISK
which is 0x97
.
Why do certain keycodes match up and not others. Most do thouh. If I just do a character compare that works( == '*')
, but I'm not sure if this the best solution?
Thank you for all help!!!
An event which indicates that a keystroke occurred in a component. This low-level event is generated by a component object (such as a text field) when a key is pressed, released, or typed.
A KeyEvent is generated when keyboard input occurs. There are three types of key events, which are identified by these integer constants: KEY_PRESSED, KEY_RELEASED, and KEY_TYPED. A KeyEvent is generated when keyboard input occurs.
KeyEvent can be used to specify the detailed action, for example: ACTION_DOWN the key has been pressed down but not released. ACTION_UP the key has just been released.
The KeyListener interface is found in java. awt. event package, and it has three methods.
OK, you're misunderstanding something.
Keys are keys, and symbols are symbols. Symbols are results of key presses, and the same key can result in different symbols depending on circumstances (key combinations like Alt, Control, Shift etc).
So, VK_8
key code stands for the key that can produce symbols 8
, *
and possibly others depending on keyboard localization.
And the * dedicated key on numeric keyboard is VK_MULTIPLY
- it can produce just one symbol *
(to my knowledge).
You probably shouldn't care about the key that the user pressed, but about the symbol that this user action produced.
This info you can get with getKeyChar()
, but please note that if the user presses Shift 8 combination to produce *
it's actually two keys (Shift and 8) and you will get two events, and the first one (for Shift) will produce an unreadable symbol.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With