Skip to content

Commit

Permalink
8320773: [macOS] All IME input blocked
Browse files Browse the repository at this point in the history
Reviewed-by: angorya, kcr, jpereda
  • Loading branch information
Martin Fox authored and Andy Goryachev committed Nov 29, 2023
1 parent b80ec39 commit c46c172
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -799,8 +799,14 @@ + (void)registerKeyEvent:(NSEvent*)event
[keyCodeForCharMap setObject:mapObject forKey:[event characters]];
// getKeyCodeForChar should not just match against a character the user types
// directly but any other character printed on the same key.
[keyCodeForCharMap setObject:mapObject forKey:[event charactersByApplyingModifiers: 0]];
[keyCodeForCharMap setObject:mapObject forKey:[event charactersByApplyingModifiers: NSEventModifierFlagShift]];
NSString* unshifted = GetStringForMacKey(event.keyCode, false);
if (unshifted != nil) {
[keyCodeForCharMap setObject:mapObject forKey:unshifted];
}
NSString* shifted = GetStringForMacKey(event.keyCode, true);
if (shifted != nil) {
[keyCodeForCharMap setObject:mapObject forKey:shifted];
}
// On some European keyboards there are useful symbols which are only
// accessible via the Option key. We don't query for the Option key
// character because on most layouts just about every key has some
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jint GetJavaKeyCodeFor(unsigned short keyCode);
jint GetJavaKeyCode(NSEvent *event);
jcharArray GetJavaKeyChars(JNIEnv *env, NSEvent *event);
NSString* GetStringForJavaKey(jchar key);
NSString* GetStringForMacKey(unsigned short keyCode, bool shifted);

// for key event injection
BOOL GetMacKey(jint javaKeyCode, unsigned short *outMacKeyCode);
21 changes: 21 additions & 0 deletions modules/javafx.graphics/src/main/native-glass/mac/GlassKey.m
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,27 @@ BOOL GetMacKey(jint javaKeyCode, unsigned short *outMacKeyCode)

}

NSString* GetStringForMacKey(unsigned short keyCode, bool shifted)
{
// Restrict to printable characters. UCKeyTranslate can produce
// odd results with keys like Home, Up Arrow, etc.
if (!macKeyCodeIsLayoutSensitive(keyCode)) return nil;

TISInputSourceRef keyboard = TISCopyCurrentKeyboardLayoutInputSource();
if (keyboard == NULL) return nil;

UInt32 modifiers = (shifted ? shiftKey : 0);
UniChar unicode[8];
UniCharCount length = queryKeyboard(keyboard, keyCode, modifiers, unicode, 8);
CFRelease(keyboard);

if (length == 1) {
return [NSString stringWithCharacters: &unicode[0] length: 1];
}

return nil;
}

/*
* Class: com_sun_glass_ui_mac_MacApplication
* Method: _getKeyCodeForChar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,9 @@ - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
- (void)keyDown:(NSEvent *)theEvent
{
KEYLOG("keyDown");
[GlassApplication registerKeyEvent:theEvent];

if (![[self inputContext] handleEvent:theEvent] || shouldProcessKeyEvent) {
[GlassApplication registerKeyEvent:theEvent];
[self->_delegate sendJavaKeyEvent:theEvent isDown:YES];
}
shouldProcessKeyEvent = YES;
Expand Down

1 comment on commit c46c172

@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.