javaswingkey-events

Creating KeyEvent with specific fields


How would I go about changing the fields of a generated KeyEvent?

Actual KeyEvent from keyboard

java.awt.event.KeyEvent[KEY_PRESSED,
keyCode=65,
keyText=A,
keyChar='a',
keyLocation=KEY_LOCATION_STANDARD,
rawCode=65,
primaryLevelUnicode=97,
scancode=30,
extendedKeyCode=0x41] on panel0

Generated KeyEvent

java.awt.event.KeyEvent[KEY_PRESSED,
keyCode=65,
keyText=A,
keyChar='a',
keyLocation=KEY_LOCATION_STANDARD,
rawCode=0,
primaryLevelUnicode=0,
scancode=0,
extendedKeyCode=0x0] on panel0

new KeyEvent(component, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, 65, 'a')

Difference

rawCode
primaryLevelUnicode
scancode
extendedKeyCode

Is there a way I can set those fields on a KeyEvent object?

Docs https://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html


Solution

  • Looking at the source in the KeyEvent class, it appears you cannot set those:

    //set from native code.
    private transient long rawCode = 0;
    private transient long primaryLevelUnicode = 0;
    private transient long scancode = 0; // for MS Windows only
    private transient long extendedKeyCode = 0;
    

    It's private, and set from native code (so no changing in Java).