Skip to content

Commit

Permalink
8087700: [KeyCombination, Mac] KeyCharacterCombinations behave errati…
Browse files Browse the repository at this point in the history
…cally

Reviewed-by: angorya, kcr
  • Loading branch information
Martin Fox authored and kevinrushforth committed Nov 16, 2023
1 parent 2e73304 commit cda623d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

#import "ProcessInfo.h"
#import <Security/SecRequirement.h>
#import <Carbon/Carbon.h>

//#define VERBOSE
#ifndef VERBOSE
Expand Down Expand Up @@ -774,13 +775,38 @@ + (void)leaveFullScreenExitingLoopIfNeeded
javaIDs.Application.leaveNestedEventLoop, (jobject)NULL);
}

static void inputDidChangeCallback(CFNotificationCenterRef center, void *observer, CFNotificationName name, const void *object, CFDictionaryRef userInfo)
{
if (keyCodeForCharMap != nil) {
[keyCodeForCharMap removeAllObjects];
}
}

+ (void)registerKeyEvent:(NSEvent*)event
{
if (!keyCodeForCharMap) {
keyCodeForCharMap = [[NSMutableDictionary alloc] initWithCapacity:100];
// Note: it's never released, just like, say, the jApplication reference...
// To avoid stale entries when the user switches layout
CFNotificationCenterRef center = CFNotificationCenterGetDistributedCenter();
CFNotificationCenterAddObserver(center, NULL, inputDidChangeCallback,
kTISNotifySelectedKeyboardInputSourceChanged,
NULL, CFNotificationSuspensionBehaviorCoalesce);
}
[keyCodeForCharMap setObject:[NSNumber numberWithUnsignedShort:[event keyCode]] forKey:[event characters]];

// Add the character the user typed to the map.
NSNumber* mapObject = [NSNumber numberWithUnsignedShort:[event keyCode]];
[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]];
// 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
// random symbol assigned to that modifier which the user probably
// isn't even aware of. The user can get that character into the map
// by typing it directly.
}

+ (jint)getKeyCodeForChar:(jchar)c;
Expand Down
28 changes: 18 additions & 10 deletions tests/manual/events/KeyboardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
public class KeyboardTest extends Application {

public static void main(String[] args) {
Application.launch(args);
Application.launch(KeyboardTest.class, args);
}

private static final String os = System.getProperty("os.name");
Expand Down Expand Up @@ -167,7 +167,7 @@ private void add(KeyCode cd) {
list.add(new KeyData(cd, null, null, null));
}

/* Add a key with unshifted, shifted, and AltGr/Option characters */
/* Add a key with unshifted, shifted, and AltGr characters */
private void add(KeyCode cd, String base, String shifted, String altGr) {
list.add(new KeyData(cd, base, shifted, altGr));
}
Expand Down Expand Up @@ -249,9 +249,9 @@ private void addMiscellaneous() {

/*
* ENTER is assigned to both Return and Enter which generate
* different characters.
* different characters. Platforms should always target Return.
*/
add(KeyCode.ENTER, "wild");
add(KeyCode.ENTER, "\r");

if (onMac) {
add(KeyCode.COMMAND);
Expand Down Expand Up @@ -334,8 +334,8 @@ public static KeyList frenchKeys() {
builder.addCommon();
builder.addLetters();

/* Include one combination that involves AltGr/Option. */
final String altGrFive = (onMac ? "{" : "[");
/* Include one combination that involves AltGr. */
final String altGrFive = "[";

/*
* On a French layout the unshifted top-row keys (which generate
Expand All @@ -356,15 +356,16 @@ public static KeyList frenchKeys() {
builder.add(KeyCode.DIGIT2, E_ACUTE, "2");
builder.add(KeyCode.DIGIT3, DOUBLE_QUOTE, "3");
builder.add(KeyCode.DIGIT4, QUOTE, "4");
builder.add(KeyCode.DIGIT5, "(", "5", altGrFive);
/* Six and eight require some tweaking, below */
/* Five, six, and eight require some tweaking, below */
builder.add(KeyCode.DIGIT7, E_GRAVE, "7");
builder.add(KeyCode.DIGIT9, C_CEDILLA, "9");

if (onMac) {
builder.add(KeyCode.DIGIT5, "(", "5");
builder.add(KeyCode.DIGIT6, SECTION, "6");
builder.add(KeyCode.DIGIT8, "!", "8");
} else {
builder.add(KeyCode.DIGIT5, "(", "5", altGrFive);
builder.add(KeyCode.DIGIT6, "-", "6");
builder.add(KeyCode.DIGIT8, "_", "8");
}
Expand Down Expand Up @@ -409,7 +410,14 @@ public static KeyList germanKeys() {
builder.add(KeyCode.DIGIT4, "4", "$");
builder.add(KeyCode.DIGIT5, "5", "%");
builder.add(KeyCode.DIGIT6, "6", "&");
builder.add(KeyCode.DIGIT7, "7", "/", altGrSeven);

if (onMac) {
builder.add(KeyCode.DIGIT7, "7", "/");
}
else {
builder.add(KeyCode.DIGIT7, "7", "/", "{");
}

builder.add(KeyCode.DIGIT8, "8", "(");
builder.add(KeyCode.DIGIT9, "9", ")");

Expand Down Expand Up @@ -448,7 +456,7 @@ public static KeyList spanishKeys() {

builder.add(KeyCode.QUOTE, QUOTE, "?");
builder.add(KeyCode.INVERTED_EXCLAMATION_MARK, INV_EXCLAMATION_MARK, INV_QUESTION_MARK);
builder.add(KeyCode.PLUS, "+", "*", "]");
builder.add(KeyCode.PLUS, "+", "*");
builder.add(KeyCode.LESS, "<", ">");
builder.add(KeyCode.COMMA, ",", ";");
builder.add(KeyCode.PERIOD, ".", ":");
Expand Down

3 comments on commit cda623d

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@kevinrushforth
Copy link
Member

Choose a reason for hiding this comment

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

/tag 22+18

@openjdk
Copy link

@openjdk openjdk bot commented on cda623d Nov 16, 2023

Choose a reason for hiding this comment

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

@kevinrushforth The tag 22+18 was successfully created.

Please sign in to comment.