8155740: [macosx] robot.keyPress and robot.keyRelease do not generate key event for Alt-Graph key VK_ALT_GRAPH
authormhalder
Wed, 22 Jun 2016 16:10:36 +0530
changeset 39526 3d0ccf1d8dc3
parent 39525 bc04e7b75111
child 39527 91a2d9219e81
8155740: [macosx] robot.keyPress and robot.keyRelease do not generate key event for Alt-Graph key VK_ALT_GRAPH Reviewed-by: ssadetsky, serb
jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTEvent.m
jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/CRobot.m
jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/CRobotKeyCode.m
jdk/test/java/awt/keyboard/AllKeyCode/AllKeyCode.java
--- a/jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTEvent.m	Wed Jun 22 12:36:18 2016 +0300
+++ b/jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTEvent.m	Wed Jun 22 16:10:36 2016 +0530
@@ -131,7 +131,7 @@
     {0x3A, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_ALT},       // ****
     {0x3B, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_CONTROL},   // ****
     {0x3C, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
-    {0x3D, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
+    {0x3D, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_ALT_GRAPH},
     {0x3E, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
     {0x3F, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED}, // the 'fn' key on PowerBooks
     {0x40, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F17},
@@ -293,18 +293,18 @@
         61,
         java_awt_event_InputEvent_ALT_DOWN_MASK | java_awt_event_InputEvent_ALT_GRAPH_DOWN_MASK,
         java_awt_event_InputEvent_ALT_MASK | java_awt_event_InputEvent_ALT_GRAPH_MASK,
-        java_awt_event_KeyEvent_VK_ALT
+        java_awt_event_KeyEvent_VK_ALT | java_awt_event_KeyEvent_VK_ALT_GRAPH
     },
-    {
-        NSCommandKeyMask,
-        //kCGSFlagsMaskAppleLeftCommandKey,
-        //kCGSFlagsMaskAppleRightCommandKey,
-        55,
-        54,
-        java_awt_event_InputEvent_META_DOWN_MASK,
-        java_awt_event_InputEvent_META_MASK,
-        java_awt_event_KeyEvent_VK_META
-    },
+	{
+		NSCommandKeyMask,
+		//kCGSFlagsMaskAppleLeftCommandKey,
+		//kCGSFlagsMaskAppleRightCommandKey,
+		55,
+		54,
+		java_awt_event_InputEvent_META_DOWN_MASK,
+		java_awt_event_InputEvent_META_MASK,
+		java_awt_event_KeyEvent_VK_META
+	},
     // NSNumericPadKeyMask
     {
         NSHelpKeyMask,
@@ -554,20 +554,20 @@
  */
 jint NsKeyModifiersToJavaModifiers(NSUInteger nsFlags, BOOL isExtMods)
 {
-    jint javaModifiers = 0;
-    const struct _nsKeyToJavaModifier* cur;
+	jint javaModifiers = 0;
+	const struct _nsKeyToJavaModifier* cur;
+	
+	for (cur = nsKeyToJavaModifierTable; cur->nsMask != 0; ++cur) {
+		if ((cur->nsMask & nsFlags) != 0) {
+				javaModifiers |= isExtMods ? cur->javaExtMask : cur->javaMask;
+				if (cur->nsMask == NSAlternateKeyMask && leftAltKeyPressed == NO) {
+					continue;
+			}
+			break;
+		}
+	}
 
-    for (cur = nsKeyToJavaModifierTable; cur->nsMask != 0; ++cur) {
-        if ((cur->nsMask & nsFlags) != 0) {
-            javaModifiers |= isExtMods ? cur->javaExtMask : cur->javaMask;
-            if (cur->nsMask == NSAlternateKeyMask && leftAltKeyPressed == NO) {
-                continue;
-            }
-            break;
-        }
-    }
-
-    return javaModifiers;
+	return javaModifiers;
 }
 
 /*
--- a/jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/CRobot.m	Wed Jun 22 12:36:18 2016 +0300
+++ b/jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/CRobot.m	Wed Jun 22 16:10:36 2016 +0530
@@ -33,6 +33,7 @@
 #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.
@@ -258,26 +259,30 @@
 Java_sun_lwawt_macosx_CRobot_keyEvent
 (JNIEnv *env, jobject peer, jint javaKeyCode, jboolean keyPressed)
 {
-    /*
-     * Well, using CGEventCreateKeyboardEvent/CGEventPost would have been
-     * 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);
 
-
-#if 0
-    CGEventRef event = CGEventCreateKeyboardEvent(NULL, keyCode, keyPressed);
-    if (event != NULL) {
-        CGEventPost(kCGSessionEventTap, event);
-        CFRelease(event);
+    if ((javaKeyCode >= java_awt_event_KeyEvent_VK_0) &&
+		(javaKeyCode <= java_awt_event_KeyEvent_VK_9))
+    {
+        AXUIElementRef elem = AXUIElementCreateSystemWide();
+        AXUIElementPostKeyboardEvent(elem, (CGCharCode)0, keyCode, keyPressed);
+        CFRelease(elem);
+    } else {
+        /*
+         * JDK-8155740: AXUIElementPostKeyboardEvent posts correct key codes for
+         * number keys whereas CGEventPost posts Numpad keys for corresponding
+         * number key. Hence AXUIElementPostKeyboardEvent is used for posting
+         * numbers and CGEventCreateKeyboardEvent/CGEventPost is used for other
+         * keys.
+         * Key code for modifier key is required to distinguish between ALT and
+         * ALT-GR key for fixing issue 8155740.
+         */
+        CGEventRef event = CGEventCreateKeyboardEvent(NULL, keyCode, keyPressed);
+        if (event != NULL) {
+            CGEventPost(kCGSessionEventTap, event);
+            CFRelease(event);
+        }
     }
-#endif
 }
 
 /*
--- a/jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/CRobotKeyCode.m	Wed Jun 22 12:36:18 2016 +0300
+++ b/jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/CRobotKeyCode.m	Wed Jun 22 16:10:36 2016 +0530
@@ -53,6 +53,7 @@
             [NSNumber numberWithInt : OSX_Shift], [NSNumber numberWithInt : java_awt_event_KeyEvent_VK_SHIFT],
             [NSNumber numberWithInt : OSX_Control], [NSNumber numberWithInt : java_awt_event_KeyEvent_VK_CONTROL],
             [NSNumber numberWithInt : OSX_Option], [NSNumber numberWithInt : java_awt_event_KeyEvent_VK_ALT],
+            [NSNumber numberWithInt : OSX_RightOption], [NSNumber numberWithInt : java_awt_event_KeyEvent_VK_ALT_GRAPH],
             [NSNumber numberWithInt : OSX_CapsLock], [NSNumber numberWithInt : java_awt_event_KeyEvent_VK_CAPS_LOCK],
             [NSNumber numberWithInt : OSX_Escape], [NSNumber numberWithInt : java_awt_event_KeyEvent_VK_ESCAPE],
             [NSNumber numberWithInt : OSX_kVK_Space], [NSNumber numberWithInt : java_awt_event_KeyEvent_VK_SPACE],
--- a/jdk/test/java/awt/keyboard/AllKeyCode/AllKeyCode.java	Wed Jun 22 12:36:18 2016 +0300
+++ b/jdk/test/java/awt/keyboard/AllKeyCode/AllKeyCode.java	Wed Jun 22 16:10:36 2016 +0530
@@ -22,15 +22,14 @@
  */
 
 /*
-  @test
-  @bug 8149456 8147834 8150230
-  @requires os.family == "mac"
-  @summary KeyEvents for all keys
-  @run main AllKeyCode
-*/
+ @test
+ @bug 8149456 8147834 8150230 8155740
+ @requires os.family == "mac"
+ @summary Tests key codes for all keys supported in Java for Mac OS X.
+ @run main AllKeyCode
+ */
 
 import java.awt.AWTException;
-import java.awt.GridBagLayout;
 import java.awt.Robot;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
@@ -149,7 +148,8 @@
             KeyEvent.VK_F17,
             KeyEvent.VK_F18,
             KeyEvent.VK_F19,
-            KeyEvent.VK_F20
+            KeyEvent.VK_F20,
+            KeyEvent.VK_ALT_GRAPH
         };
 
         keyPressedIndex = -1;