< prev index next >

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

Print this page

        

@@ -31,10 +31,11 @@
 
 #import "CRobotKeyCode.h"
 #import "LWCToolkit.h"
 #import "sun_lwawt_macosx_CRobot.h"
 #import "java_awt_event_InputEvent.h"
+#import "java_awt_event_KeyEvent.h"
 #import "sizecalc.h"
 
 // Starting number for event numbers generated by Robot.
 // Apple docs don't mention at all what are the requirements
 // for these numbers. It seems that they must be higher

@@ -72,10 +73,12 @@
 static void PostMouseEvent(const CGPoint point, CGMouseButton button,
                            CGEventType type, int clickCount, int eventNumber);
 
 static int GetClickCount(BOOL isDown);
 
+static BOOL IsModifierKey(jint javaKeyCode);
+
 static void
 CreateJavaException(JNIEnv* env, CGError err)
 {
     // Throw a java exception indicating what is wrong.
     NSString* s = [NSString stringWithFormat:@"Robot: CGError: %d", err];

@@ -263,23 +266,33 @@
      * a better solution, however, it gives me all kinds of trouble and I have
      * no idea how to solve them without inserting delays between simulated
      * events. So, I've ended up disabling it and opted for another approach
      * that uses Accessibility API instead.
      */
-    CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
-    AXUIElementRef elem = AXUIElementCreateSystemWide();
-    AXUIElementPostKeyboardEvent(elem, (CGCharCode)0, keyCode, keyPressed);
-    CFRelease(elem);
 
+    CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
 
-#if 0
+    /*
+     * JDK-8155740: AXUIElementPostKeyboardEvent posts 0 key code for all
+     * the modifier keys with key codes (16, 17,18, 20, 157) and also for
+     * newly added modifier key VK_ALT_GRAPH. But it posts correct key code
+     * for all the other keys. On the other hand CGEventCreateKeyboardEvent
+     * posts correct key code for all the modifier keys and hence it is used
+     * to post modifier key events and AXUIElementPostKeyboardEvent is used to
+     * post all the remaining key events.
+     */
+     if (IsModifierKey(javaKeyCode)) {
     CGEventRef event = CGEventCreateKeyboardEvent(NULL, keyCode, keyPressed);
     if (event != NULL) {
         CGEventPost(kCGSessionEventTap, event);
         CFRelease(event);
     }
-#endif
+    } else {
+        AXUIElementRef elem = AXUIElementCreateSystemWide();
+        AXUIElementPostKeyboardEvent(elem, (CGCharCode)0, keyCode, keyPressed);
+        CFRelease(elem);
+    }
 }
 
 /*
  * Class:     sun_lwawt_macosx_CRobot
  * Method:    nativeGetScreenPixels

@@ -382,5 +395,20 @@
         }
     }
 
     return gsClickCount;
 }
+
+static BOOL IsModifierKey(jint javaKeyCode) {
+        
+    switch (javaKeyCode) {
+        case java_awt_event_KeyEvent_VK_SHIFT:
+        case java_awt_event_KeyEvent_VK_CONTROL:
+        case java_awt_event_KeyEvent_VK_ALT:
+        case java_awt_event_KeyEvent_VK_CAPS_LOCK:
+        case java_awt_event_KeyEvent_VK_META:
+        case java_awt_event_KeyEvent_VK_ALT_GRAPH:
+            return YES;
+    }
+
+    return NO;
+}
< prev index next >