KeyEvent characters
Asked Answered
C

3

6

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!!!

Candler answered 27/2, 2012 at 14:57 Comment(0)
I
6

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.

Inharmonic answered 27/2, 2012 at 15:14 Comment(1)
That last suggestion seemed to be working for me. I get a 0xFFFF which is an CHAR_UNDEFIEND for the shift which I just ignore and then I do a getKeyChar() the next time I'm donig a getKeyChar() == '*' and that seems to work.Thanks!!Candler
F
2

depends of JComponent

if you want to determine from JTextComponent then use DocumentListener, if you want to modify Char sequence then use DocumentFilter

otherwise look at KeyBindings, because KeyListener is designated for AWT Components

Freddiefreddy answered 27/2, 2012 at 15:3 Comment(2)
It just a JTextField I'm using. Is there anything wrong with leaving it as getKetChar() =='*' ? Will not that always work?Candler
for a JTextField use only DocumentListener/DocumentFilter, there isn't another correct wayFreddiefreddy
H
2
  1. KeyEvent.VK_ASTERISK is a constant reserved for the (virtual) keyboard key that has * marked on it. It does not have to match the character that is produced when someone pushes the key.

    If you want to use the VK constants you have to listen for key up and key down events and use the KeyEvent.getKeyCode() method. If you want to use characters you have listen for key typed events and use the KeyEvent.getKeyChar() method.

    Some key codes may match the character just because it's easier to define it that way; there is no real need to do it that way.

  2. You say that you got a VK_8 from getKeyCode for typing the asterisk. Did you push an actual asterisk key or did you have to type Shift-8?

Hydric answered 27/2, 2012 at 15:16 Comment(1)
when I'm pressing the * on numeric keyboard I'm getting VK_MULTIPLY and not VK_ASTERISKInharmonic

© 2022 - 2024 — McMap. All rights reserved.