Skip to content

Commit 8359760

Browse files
authored
Merge 6dbbc9e into 8635642
2 parents 8635642 + 6dbbc9e commit 8359760

File tree

4 files changed

+346
-7
lines changed

4 files changed

+346
-7
lines changed

src/java.desktop/macosx/native/libawt_lwawt/awt/CRobot.m

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -63,8 +63,13 @@
6363
static int gsEventNumber;
6464
static int* gsButtonEventNumber;
6565
static NSTimeInterval gNextKeyEventTime;
66+
static CGEventFlags initFlags;
67+
static CGEventFlags allModifiersMask = kCGEventFlagMaskShift | kCGEventFlagMaskControl
68+
| kCGEventFlagMaskAlternate | kCGEventFlagMaskCommand
69+
| kCGEventFlagMaskAlphaShift | kCGEventFlagMaskSecondaryFn;
6670

6771
static inline CGKeyCode GetCGKeyCode(jint javaKeyCode);
72+
static inline int GetCGKeyMask(int cgKeyCode);
6873

6974
static void PostMouseEvent(const CGPoint point, CGMouseButton button,
7075
CGEventType type, int clickCount, int eventNumber);
@@ -121,6 +126,13 @@ static inline void autoDelay(BOOL isMove) {
121126
jboolean copy = JNI_FALSE;
122127

123128
setupDone = 1;
129+
// initialize CGEventFlags here - which is used in keyEvent
130+
initFlags = CGEventSourceFlagsState(kCGEventSourceStateHIDSystemState);
131+
132+
// Clear Function flag bits if they are set
133+
if ((initFlags & kCGEventFlagMaskSecondaryFn) != 0) {
134+
initFlags ^= kCGEventFlagMaskSecondaryFn;
135+
}
124136
// Don't block local events after posting ours
125137
CGSetLocalEventsSuppressionInterval(0.0);
126138

@@ -291,12 +303,25 @@ static inline void autoDelay(BOOL isMove) {
291303
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
292304
CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
293305
CGEventRef event = CGEventCreateKeyboardEvent(source, keyCode, keyPressed);
306+
294307
if (event != NULL) {
295-
CGEventFlags flags = CGEventSourceFlagsState(kCGEventSourceStateHIDSystemState);
296-
if ((flags & kCGEventFlagMaskSecondaryFn) != 0) {
297-
flags ^= kCGEventFlagMaskSecondaryFn;
298-
CGEventSetFlags(event, flags);
308+
int flagMaskValue = GetCGKeyMask(keyCode);
309+
if (OSX_Undefined != flagMaskValue) {
310+
if (keyCode == OSX_CapsLock) {
311+
if (keyPressed) {
312+
initFlags ^= flagMaskValue;
313+
}
314+
} else {
315+
initFlags = keyPressed
316+
? (initFlags | flagMaskValue) // add flag bits if modifier key pressed
317+
: (initFlags & ~flagMaskValue); // clear flag bits if modifier key released
318+
}
299319
}
320+
321+
CGEventFlags flags = CGEventSourceFlagsState(kCGEventSourceStateHIDSystemState);
322+
flags = (initFlags & allModifiersMask) | (flags & (~allModifiersMask));
323+
CGEventSetFlags(event, flags);
324+
300325
CGEventPost(kCGHIDEventTap, event);
301326
CFRelease(event);
302327
}
@@ -398,6 +423,12 @@ static inline CGKeyCode GetCGKeyCode(jint javaKeyCode)
398423
return [keyCodeMapping getOSXKeyCodeForJavaKey:javaKeyCode];
399424
}
400425

426+
static inline int GetCGKeyMask(int cgKeyCode)
427+
{
428+
CRobotKeyCodeMapping *keyCodeMapping = [CRobotKeyCodeMapping sharedInstance];
429+
return [keyCodeMapping getFlagMaskForCGKey:cgKeyCode];
430+
}
431+
401432
static int GetClickCount(BOOL isDown) {
402433
NSTimeInterval now = [[NSDate date] timeIntervalSinceReferenceDate];
403434
NSTimeInterval clickInterval = now - gsLastClickTime;

src/java.desktop/macosx/native/libawt_lwawt/awt/CRobotKeyCode.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -148,9 +148,11 @@ const static int OSX_Undefined = 0x7F;
148148
}
149149

150150
@property (readwrite, retain) NSDictionary *javaToMacKeyMap;
151+
@property (readwrite, retain) NSDictionary *modifierKeyToMaskMap;
151152

152153
+ (CRobotKeyCodeMapping *)sharedInstance ;
153154
- (int)getOSXKeyCodeForJavaKey:(int) javaKey;
155+
- (int)getFlagMaskForCGKey:(int) cgKeyCode;
154156

155157
@end
156158

src/java.desktop/macosx/native/libawt_lwawt/awt/CRobotKeyCode.m

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,7 @@
2929
@implementation CRobotKeyCodeMapping
3030

3131
@synthesize javaToMacKeyMap;
32+
@synthesize modifierKeyToMaskMap;
3233

3334
+(CRobotKeyCodeMapping *) sharedInstance {
3435
static CRobotKeyCodeMapping *instance = nil;
@@ -160,6 +161,15 @@ -(id) init {
160161
[NSNumber numberWithInt : OSX_F19], [NSNumber numberWithInt : java_awt_event_KeyEvent_VK_F19],
161162
[NSNumber numberWithInt : OSX_F20], [NSNumber numberWithInt : java_awt_event_KeyEvent_VK_F20],
162163

164+
nil];
165+
166+
self.modifierKeyToMaskMap = [NSDictionary dictionaryWithObjectsAndKeys:
167+
[NSNumber numberWithInt : kCGEventFlagMaskShift], [NSNumber numberWithInt : OSX_Shift],
168+
[NSNumber numberWithInt : kCGEventFlagMaskControl], [NSNumber numberWithInt : OSX_Control],
169+
[NSNumber numberWithInt : kCGEventFlagMaskAlternate], [NSNumber numberWithInt : OSX_Option],
170+
[NSNumber numberWithInt : kCGEventFlagMaskCommand], [NSNumber numberWithInt : OSX_Command],
171+
[NSNumber numberWithInt : kCGEventFlagMaskAlphaShift], [NSNumber numberWithInt : OSX_CapsLock],
172+
163173
nil];
164174
}
165175

@@ -176,4 +186,14 @@ -(int) getOSXKeyCodeForJavaKey : (int) javaKey {
176186
}
177187
}
178188

189+
-(int) getFlagMaskForCGKey : (int) cgKeyCode {
190+
id val = [modifierKeyToMaskMap objectForKey : [NSNumber numberWithInt : cgKeyCode]];
191+
192+
if (nil != val) {
193+
return [val intValue];
194+
} else {
195+
return OSX_Undefined;
196+
}
197+
}
198+
179199
@end

0 commit comments

Comments
 (0)