Дата публикации: 12.03.2019 18:58:29
Today I want to consider more complicated cases of inconsistent modifier key processing on different platforms (JDK-8218917).
First, Windows constantly sends SHIFT_PRESSED events if you press and hold the Shift key. These repeating events affect the EDT very much. I do not recall such a behaviour in Windows versions earlier than 7. If you disable this functionality on the OS level, then other keys, such as arrows or letters, will not be auto-repeated too. However, in this mode you can notice that some combinations of modifier keys are processed incorrectly.
Consider the Control and the Shift keys, which send the expected events in both cases.
Press CTRL, press SHIFT, release SHIFT, release CTRL: KeyEvent[KEY_PRESSED,keyCode=17,keyText=Ctrl,modifiers=Ctrl,extModifiers=Ctrl,keyLocation=KEY_LOCATION_LEFT,rawCode=17,primaryLevelUnicode=0,scancode=29,extendedKeyCode=0x11] KeyEvent[KEY_PRESSED,keyCode=16,keyText=Shift,modifiers=Ctrl+Shift,extModifiers=Ctrl+Shift,keyLocation=KEY_LOCATION_RIGHT,rawCode=16,primaryLevelUnicode=0,scancode=42,extendedKeyCode=0x10] KeyEvent[KEY_RELEASED,keyCode=16,keyText=Shift,modifiers=Ctrl,extModifiers=Ctrl,keyLocation=KEY_LOCATION_RIGHT,rawCode=16,primaryLevelUnicode=0,scancode=42,extendedKeyCode=0x10] KeyEvent[KEY_RELEASED,keyCode=17,keyText=Ctrl,keyLocation=KEY_LOCATION_LEFT,rawCode=17,primaryLevelUnicode=0,scancode=29,extendedKeyCode=0x11]
Press CTRL, press SHIFT, release CTRL, release SHIFT: KeyEvent[KEY_PRESSED,keyCode=17,keyText=Ctrl,modifiers=Ctrl,extModifiers=Ctrl,keyLocation=KEY_LOCATION_LEFT,rawCode=17,primaryLevelUnicode=0,scancode=29,extendedKeyCode=0x11] KeyEvent[KEY_PRESSED,keyCode=16,keyText=Shift,modifiers=Ctrl+Shift,extModifiers=Ctrl+Shift,keyLocation=KEY_LOCATION_RIGHT,rawCode=16,primaryLevelUnicode=0,scancode=42,extendedKeyCode=0x10] KeyEvent[KEY_RELEASED,keyCode=17,keyText=Ctrl,modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_LEFT,rawCode=17,primaryLevelUnicode=0,scancode=29,extendedKeyCode=0x11] KeyEvent[KEY_RELEASED,keyCode=16,keyText=Shift,keyLocation=KEY_LOCATION_RIGHT,rawCode=16,primaryLevelUnicode=0,scancode=42,extendedKeyCode=0x10]
Now try the Alt and the Shift keys. They send the expected events only in the first case. In the second case, an unexpected CTRL_RELEASED event appears, although I have not pressed any Ctrl key.
Press ALT, press SHIFT, release SHIFT, release ALT: KeyEvent[KEY_PRESSED,keyCode=18,keyText=Alt,modifiers=Alt,extModifiers=Alt,keyLocation=KEY_LOCATION_LEFT,rawCode=18,primaryLevelUnicode=0,scancode=56,extendedKeyCode=0x12] KeyEvent[KEY_PRESSED,keyCode=16,keyText=Shift,modifiers=Alt+Shift,extModifiers=Alt+Shift,keyLocation=KEY_LOCATION_LEFT,rawCode=16,primaryLevelUnicode=0,scancode=42,extendedKeyCode=0x10] KeyEvent[KEY_RELEASED,keyCode=16,keyText=Shift,modifiers=Alt,extModifiers=Alt,keyLocation=KEY_LOCATION_LEFT,rawCode=16,primaryLevelUnicode=0,scancode=42,extendedKeyCode=0x10] KeyEvent[KEY_RELEASED,keyCode=18,keyText=Alt,keyLocation=KEY_LOCATION_LEFT,rawCode=18,primaryLevelUnicode=0,scancode=56,extendedKeyCode=0x12]
Press ALT, press SHIFT, release ALT, release SHIFT: KeyEvent[KEY_PRESSED,keyCode=18,keyText=Alt,modifiers=Alt,extModifiers=Alt,keyLocation=KEY_LOCATION_LEFT,rawCode=18,primaryLevelUnicode=0,scancode=56,extendedKeyCode=0x12] KeyEvent[KEY_PRESSED,keyCode=16,keyText=Shift,modifiers=Alt+Shift,extModifiers=Alt+Shift,keyLocation=KEY_LOCATION_LEFT,rawCode=16,primaryLevelUnicode=0,scancode=42,extendedKeyCode=0x10] KeyEvent[KEY_RELEASED,keyCode=18,keyText=Alt,modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_LEFT,rawCode=18,primaryLevelUnicode=0,scancode=56,extendedKeyCode=0x12] KeyEvent[KEY_RELEASED,keyCode=17,keyText=Ctrl,modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_LEFT,rawCode=17,primaryLevelUnicode=0,scancode=29,extendedKeyCode=0x11] KeyEvent[KEY_RELEASED,keyCode=16,keyText=Shift,keyLocation=KEY_LOCATION_LEFT,rawCode=16,primaryLevelUnicode=0,scancode=42,extendedKeyCode=0x10]
If you try to use both left and right Shift keys, the first SHIFT_RELEASED event gets lost.
Press LEFT SHIFT, press RIGHT SHIFT, release RIGHT SHIFT, release LEFT SHIFT: KeyEvent[KEY_PRESSED,keyCode=16,keyText=Shift,modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_LEFT,rawCode=16,primaryLevelUnicode=0,scancode=42,extendedKeyCode=0x10] KeyEvent[KEY_PRESSED,keyCode=16,keyText=Shift,modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_RIGHT,rawCode=16,primaryLevelUnicode=0,scancode=42,extendedKeyCode=0x10] KeyEvent[KEY_RELEASED,keyCode=16,keyText=Shift,keyLocation=KEY_LOCATION_LEFT,rawCode=16,primaryLevelUnicode=0,scancode=42,extendedKeyCode=0x10]
Second, there is the Caps Lock key, which people forget to test. However, each OS processes it its own way.
Windows behaves as expected, except for the problem described above. If you press the Caps Lock key, the CAPS_LOCK_PRESSED event is sent. If you release it, the CAPS_LOCK_RELEASED event is sent.
Linux tries to work as Windows but slightly different. If you press the Caps Lock key, the both CAPS_LOCK_PRESSED and CAPS_LOCK_RELEASED events are sent. If you release it, nothing happens.
Mac behaves differently in this case. It does not send event on the key release like Linux. If you press the Caps Lock key, the CAPS_LOCK_PRESSED event is sent. If you press this key again to turn CAPS MODE off, the CAPS_LOCK_RELEASED event is sent. It is quite strange and unexpected.