Java: keyCode, rawCode, primaryLevelUnicode, scancode, what are they?

1.2k views Asked by At

I am trying to simulate the keyboard stroke. When I press the - key, I receive the following event.

What does the various codes mean?

[KEY_PRESSED, keyCode=45, keyText=Minus, keyChar='-', keyLocation=KEY_LOCATION_STANDARD, rawCode=189, primaryLevelUnicode=45, scancode=12]

1

There are 1 answers

5
Jean-François Savard On
  • KEY_PRESSED is the event type.
  • keyCode is the code as integer of the key. Note that not all characters have a keycode associated with them. For example, there is no keycode for the question mark because there is no keyboard for which it appears on the primary layer. You can get a list of each code here.
  • keyText is the text description of the character.
  • keyChar is the typed char.
  • keyLocation is the location of the key that originated this key event. Some keys occur more than once on a keyboard, e.g. the left and right shift keys. Additionally, some keys occur on the numeric keypad. This provides a way of distinguishing such keys.
  • You can get the list of raw key code here. Note that there exist raw code for humanly typed character, and virtual key code for processed keys.
  • primaryLevelUnicode I believe this is the chart used to map the characters. Unicode have multiple levels of chart mostly usefull for comparison (collator).
  • Each time you press a key, scancode are sent to the computer to identify the key. See this answer for more details. You can get a list of scan codes here.

Note that the scancode seems to be for MS Windows only as per comment in KeyEvent:

private transient long scancode = 0; // for MS Windows only