Skip to content

Commit

Permalink
JBR-4334 java/awt/Mouse/MouseModifiersUnitTest/MouseModifiersUnitTest…
Browse files Browse the repository at this point in the history
…_Extra.java fails

re-fix JBR-4306 in a different way
  • Loading branch information
JB-Dmitry committed Mar 29, 2022
1 parent 0f33031 commit 3b5ded0
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/java.desktop/macosx/native/libawt_lwawt/awt/CRobot.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
static NSTimeInterval gNextKeyEventTime;
static NSTimeInterval safeDelay;

#define KEY_CODE_COUNT 128
static CGEventFlags keyOwnFlags[KEY_CODE_COUNT];

static inline CGKeyCode GetCGKeyCode(jint javaKeyCode);

static void PostMouseEvent(const CGPoint point, CGMouseButton button,
Expand Down Expand Up @@ -107,6 +110,20 @@ static inline void autoDelay(BOOL isMove) {
gNextKeyEventTime = [[NSDate date] timeIntervalSinceReferenceDate] + safeDelay;
}

static void initKeyFlags() {
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStatePrivate);
for (CGKeyCode keyCode = 0; keyCode < KEY_CODE_COUNT; keyCode++) {
CGEventRef event = CGEventCreateKeyboardEvent(source, keyCode, true);
if (event != NULL) {
keyOwnFlags[keyCode] = CGEventGetFlags(event);
CFRelease(event);
}
}
if (source != NULL) {
CFRelease(source);
}
}

/*
* Class: sun_lwawt_macosx_CRobot
* Method: initRobot
Expand Down Expand Up @@ -155,6 +172,8 @@ static inline void autoDelay(BOOL isMove) {
for (i = 0; i < gNumberOfButtons; ++i) {
gsButtonEventNumber[i] = ROBOT_EVENT_NUMBER_START;
}

initKeyFlags();
}];
}
}
Expand Down Expand Up @@ -283,6 +302,20 @@ static inline void autoDelay(BOOL isMove) {
}];
}

// CGEventCreateKeyboardEvent incorrectly handles flags pertinent to non-modifier keys
// (e.g. F1-F12 keys always Fn flag set, while arrow keys always have Fn and NumPad flags set).
// Those flags are not cleared for following key presses automatically, so we need to do it ourselves.
// See JBR-4306 for details.
static void clearStickyFlags(CGEventRef event, CGKeyCode keyCode, CGEventFlags flagToCheck) {
if (keyCode < KEY_CODE_COUNT && (keyOwnFlags[keyCode] & flagToCheck) == 0) {
CGEventFlags flags = CGEventGetFlags(event);
CGEventFlags updatedFlags = flags & ~flagToCheck;
if (updatedFlags != flags) {
CGEventSetFlags(event, updatedFlags);
}
}
}

/*
* Class: sun_lwawt_macosx_CRobot
* Method: keyEvent
Expand All @@ -298,6 +331,10 @@ static inline void autoDelay(BOOL isMove) {
CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
CGEventRef event = CGEventCreateKeyboardEvent(source, keyCode, keyPressed);
if (event != NULL) {
// this assumes Robot isn't used to generate Fn key presses
clearStickyFlags(event, keyCode, kCGEventFlagMaskSecondaryFn);
// there is no NumPad key, so this won't hurt in any case
clearStickyFlags(event, keyCode, kCGEventFlagMaskNumericPad);
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);
}
Expand Down

0 comments on commit 3b5ded0

Please sign in to comment.