Skip to content

Commit

Permalink
8315701: [macos] Regression: KeyEvent has different keycode on differ…
Browse files Browse the repository at this point in the history
…ent keyboard layouts

Reviewed-by: aivanov, kizune
  • Loading branch information
Alisen Chung committed Nov 30, 2023
1 parent 6aba6aa commit a3eb664
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 343 deletions.
12 changes: 8 additions & 4 deletions src/java.desktop/macosx/classes/sun/lwawt/LWWindowPeer.java
Expand Up @@ -1044,7 +1044,7 @@ public void notifyMouseWheelEvent(long when, int x, int y, int absX,
*/
@Override
public void notifyKeyEvent(int id, long when, int modifiers,
int keyCode, char keyChar, int keyLocation)
int keyCode, char keyChar, int keyLocation, int extendedKeyCode)
{
LWKeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
Component focusOwner = kfmPeer.getCurrentFocusOwner();
Expand All @@ -1058,9 +1058,13 @@ public void notifyKeyEvent(int id, long when, int modifiers,

KeyEvent keyEvent = new KeyEvent(focusOwner, id, when, modifiers,
keyCode, keyChar, keyLocation);
AWTAccessor.getKeyEventAccessor().setExtendedKeyCode(keyEvent,
(keyChar == KeyEvent.CHAR_UNDEFINED) ? keyCode
: ExtendedKeyCodes.getExtendedKeyCodeForChar(keyChar));
if (extendedKeyCode >= 0) {
AWTAccessor.getKeyEventAccessor().setExtendedKeyCode(keyEvent, extendedKeyCode);
} else {
AWTAccessor.getKeyEventAccessor().setExtendedKeyCode(keyEvent,
(keyChar == KeyEvent.CHAR_UNDEFINED) ? keyCode
: ExtendedKeyCodes.getExtendedKeyCodeForChar(keyChar));
}
postEvent(keyEvent);
}

Expand Down
Expand Up @@ -61,5 +61,5 @@ void notifyMouseWheelEvent(long when, int x, int y, final int absX,
* Called by the delegate when a key is pressed.
*/
void notifyKeyEvent(int id, long when, int modifiers,
int keyCode, char keyChar, int keyLocation);
int keyCode, char keyChar, int keyLocation, int extendedKeyCode);
}
Expand Up @@ -143,6 +143,7 @@ void handleKeyEvent(int eventType, int modifierFlags, String chars, String chars

int jeventType = KeyEvent.KEY_PRESSED;
int jkeyCode = KeyEvent.VK_UNDEFINED;
int jextendedkeyCode = -1;
int jkeyLocation = KeyEvent.KEY_LOCATION_UNKNOWN;
boolean postsTyped = false;
boolean spaceKeyTyped = false;
Expand Down Expand Up @@ -173,7 +174,7 @@ void handleKeyEvent(int eventType, int modifierFlags, String chars, String chars
charsIgnoringModifiers.charAt(0) : KeyEvent.CHAR_UNDEFINED;

int[] in = new int[] {testCharIgnoringModifiers, isDeadChar ? 1 : 0, modifierFlags, keyCode};
int[] out = new int[3]; // [jkeyCode, jkeyLocation, deadChar]
int[] out = new int[4]; // [jkeyCode, jkeyLocation, deadChar, extendedKeyCode]

postsTyped = NSEvent.nsToJavaKeyInfo(in, out);
if (!postsTyped) {
Expand Down Expand Up @@ -201,6 +202,7 @@ void handleKeyEvent(int eventType, int modifierFlags, String chars, String chars
}

jkeyCode = out[0];
jextendedkeyCode = out[3];
jkeyLocation = out[1];
jeventType = isNpapiCallback ? NSEvent.npToJavaEventType(eventType) :
NSEvent.nsToJavaEventType(eventType);
Expand All @@ -221,7 +223,7 @@ void handleKeyEvent(int eventType, int modifierFlags, String chars, String chars
lastKeyPressCode = jkeyCode;
}
eventNotifier.notifyKeyEvent(jeventType, when, jmodifiers,
jkeyCode, javaChar, jkeyLocation);
jkeyCode, javaChar, jkeyLocation, jextendedkeyCode);

// Current browser may be sending input events, so don't
// post the KEY_TYPED here.
Expand All @@ -241,12 +243,12 @@ void handleKeyEvent(int eventType, int modifierFlags, String chars, String chars
}
eventNotifier.notifyKeyEvent(KeyEvent.KEY_TYPED, when, jmodifiers,
KeyEvent.VK_UNDEFINED, javaChar,
KeyEvent.KEY_LOCATION_UNKNOWN);
KeyEvent.KEY_LOCATION_UNKNOWN, jextendedkeyCode);
//If events come from Firefox, released events should also be generated.
if (needsKeyReleased) {
eventNotifier.notifyKeyEvent(KeyEvent.KEY_RELEASED, when, jmodifiers,
jkeyCode, javaChar,
KeyEvent.KEY_LOCATION_UNKNOWN);
KeyEvent.KEY_LOCATION_UNKNOWN, jextendedkeyCode);
}
}
}
Expand All @@ -260,13 +262,13 @@ void handleInputEvent(String text) {
eventNotifier.notifyKeyEvent(KeyEvent.KEY_TYPED,
System.currentTimeMillis(),
0, KeyEvent.VK_UNDEFINED, c,
KeyEvent.KEY_LOCATION_UNKNOWN);
KeyEvent.KEY_LOCATION_UNKNOWN, -1);
index++;
}
eventNotifier.notifyKeyEvent(KeyEvent.KEY_RELEASED,
System.currentTimeMillis(),
0, lastKeyPressCode, c,
KeyEvent.KEY_LOCATION_UNKNOWN);
KeyEvent.KEY_LOCATION_UNKNOWN, -1);
}
}

Expand Down
Expand Up @@ -245,7 +245,7 @@ public void notifyMouseWheelEvent(long when, int x, int y, int absX,

@Override
public void notifyKeyEvent(int id, long when, int modifiers, int keyCode,
char keyChar, int keyLocation) {
char keyChar, int keyLocation, int jextendedkeyCode) {
}

protected int getInitialStyleBits() {
Expand Down
13 changes: 7 additions & 6 deletions src/java.desktop/macosx/native/libawt_lwawt/awt/AWTEvent.m
Expand Up @@ -429,7 +429,7 @@ static unichar NsGetDeadKeyChar(unsigned short keyCode)
NsCharToJavaVirtualKeyCode(unichar ch, BOOL isDeadChar,
NSUInteger flags, unsigned short key,
jint *keyCode, jint *keyLocation, BOOL *postsTyped,
unichar *deadChar)
unichar *deadChar, jint *extendedkeyCode)
{
static size_t size = sizeof(keyTable) / sizeof(struct _key);
NSInteger offset;
Expand Down Expand Up @@ -469,10 +469,9 @@ static unichar NsGetDeadKeyChar(unsigned short keyCode)
*postsTyped = YES;
// do quick conversion
// the keyCode is off by 32, so adding it here
*keyCode = java_awt_event_KeyEvent_VK_A + offset + 32;
*extendedkeyCode = java_awt_event_KeyEvent_VK_A + offset + 32;
*keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_STANDARD;
return;
}
}
}

if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:ch]) {
Expand Down Expand Up @@ -695,18 +694,20 @@ jlong UTC(NSEvent *event) {
jshort keyCode = (jshort)data[3];

jint jkeyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
jint jextendedkeyCode = -1;
jint jkeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
jint testDeadChar = 0;

NsCharToJavaVirtualKeyCode((unichar)testChar, isDeadChar,
(NSUInteger)modifierFlags, (unsigned short)keyCode,
&jkeyCode, &jkeyLocation, &postsTyped,
(unichar *) &testDeadChar);
(unichar *) &testDeadChar, &jextendedkeyCode);

// out = [jkeyCode, jkeyLocation, deadChar];
// out = [jkeyCode, jkeyLocation, deadChar, jextendedkeyCode];
(*env)->SetIntArrayRegion(env, outData, 0, 1, &jkeyCode);
(*env)->SetIntArrayRegion(env, outData, 1, 1, &jkeyLocation);
(*env)->SetIntArrayRegion(env, outData, 2, 1, &testDeadChar);
(*env)->SetIntArrayRegion(env, outData, 3, 1, &jextendedkeyCode);

(*env)->ReleaseIntArrayElements(env, inData, data, 0);

Expand Down

This file was deleted.

1 comment on commit a3eb664

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.