8143054: [macosx] KeyEvent modifiers do not contain information about mouse buttons
authorserb
Tue, 29 Dec 2015 16:46:11 +0300
changeset 35657 5ee7a31d7923
parent 35656 e5582272e65b
child 35658 48bccfa61aef
8143054: [macosx] KeyEvent modifiers do not contain information about mouse buttons Reviewed-by: ssadetsky, alexsch
jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java
jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CTrayIcon.java
jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/NSEvent.java
jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTEvent.m
jdk/test/java/awt/Mouse/MouseModifiersUnitTest/MouseModifiersInKeyEvent.java
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java	Tue Dec 29 16:43:17 2015 +0300
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java	Tue Dec 29 16:46:11 2015 +0300
@@ -75,8 +75,7 @@
             jclickCount = clickCount;
         }
 
-        int jmodifiers = NSEvent.nsToJavaMouseModifiers(buttonNumber,
-                                                        modifierFlags);
+        int jmodifiers = NSEvent.nsToJavaModifiers(modifierFlags);
         boolean jpopupTrigger = NSEvent.isPopupTrigger(jmodifiers);
 
         eventNotifier.notifyMouseEvent(jeventType, System.currentTimeMillis(), jbuttonNumber,
@@ -90,9 +89,7 @@
     void handleScrollEvent(final int x, final int y, final int absX,
                            final int absY, final int modifierFlags,
                            final double deltaX, final double deltaY) {
-        final int buttonNumber = CocoaConstants.kCGMouseButtonCenter;
-        int jmodifiers = NSEvent.nsToJavaMouseModifiers(buttonNumber,
-                                                        modifierFlags);
+        int jmodifiers = NSEvent.nsToJavaModifiers(modifierFlags);
         final boolean isShift = (jmodifiers & InputEvent.SHIFT_DOWN_MASK) != 0;
 
         // Vertical scroll.
@@ -187,8 +184,7 @@
             postsTyped = false;
         }
 
-
-        int jmodifiers = NSEvent.nsToJavaKeyModifiers(modifierFlags);
+        int jmodifiers = NSEvent.nsToJavaModifiers(modifierFlags);
         long when = System.currentTimeMillis();
 
         if (jeventType == KeyEvent.KEY_PRESSED) {
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CTrayIcon.java	Tue Dec 29 16:43:17 2015 +0300
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CTrayIcon.java	Tue Dec 29 16:46:11 2015 +0300
@@ -214,7 +214,7 @@
             jclickCount = nsEvent.getClickCount();
         }
 
-        int jmodifiers = NSEvent.nsToJavaMouseModifiers(buttonNumber,
+        int jmodifiers = NSEvent.nsToJavaModifiers(
                 nsEvent.getModifierFlags());
         boolean isPopupTrigger = NSEvent.isPopupTrigger(jmodifiers);
 
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/NSEvent.java	Tue Dec 29 16:43:17 2015 +0300
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/NSEvent.java	Tue Dec 29 16:46:11 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -231,16 +231,14 @@
         return jeventType;
     }
 
-    /*
-     * Converts NSEvent mouse modifiers to AWT mouse modifiers.
+    /**
+     * Converts NSEvent key modifiers to AWT key modifiers. Note that this
+     * method adds the current mouse state as a mouse modifiers.
+     *
+     * @param  modifierFlags the NSEvent key modifiers
+     * @return the java key and mouse modifiers
      */
-    static native int nsToJavaMouseModifiers(int buttonNumber,
-                                                    int modifierFlags);
-
-    /*
-     * Converts NSEvent key modifiers to AWT key modifiers.
-     */
-    static native int nsToJavaKeyModifiers(int modifierFlags);
+    static native int nsToJavaModifiers(int modifierFlags);
 
     /*
      * Converts NSEvent key info to AWT key info.
--- a/jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTEvent.m	Tue Dec 29 16:43:17 2015 +0300
+++ b/jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTEvent.m	Tue Dec 29 16:46:11 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -579,7 +579,7 @@
 }
 
 
-jint GetJavaMouseModifiers(NSInteger button, NSUInteger modifierFlags)
+jint GetJavaMouseModifiers(NSUInteger modifierFlags)
 {
     // Mousing needs the key modifiers
     jint modifiers = NsKeyModifiersToJavaModifiers(modifierFlags, YES);
@@ -632,38 +632,18 @@
 
 /*
  * Class:     sun_lwawt_macosx_NSEvent
- * Method:    nsToJavaMouseModifiers
+ * Method:    nsToJavaModifiers
  * Signature: (II)I
  */
 JNIEXPORT jint JNICALL
-Java_sun_lwawt_macosx_NSEvent_nsToJavaMouseModifiers
-(JNIEnv *env, jclass cls, jint buttonNumber, jint modifierFlags)
-{
-    jint jmodifiers = 0;
-
-JNF_COCOA_ENTER(env);
-
-    jmodifiers = GetJavaMouseModifiers(buttonNumber, modifierFlags);
-
-JNF_COCOA_EXIT(env);
-
-    return jmodifiers;
-}
-
-/*
- * Class:     sun_lwawt_macosx_NSEvent
- * Method:    nsToJavaKeyModifiers
- * Signature: (I)I
- */
-JNIEXPORT jint JNICALL
-Java_sun_lwawt_macosx_NSEvent_nsToJavaKeyModifiers
+Java_sun_lwawt_macosx_NSEvent_nsToJavaModifiers
 (JNIEnv *env, jclass cls, jint modifierFlags)
 {
     jint jmodifiers = 0;
 
 JNF_COCOA_ENTER(env);
 
-    jmodifiers = NsKeyModifiersToJavaModifiers(modifierFlags, YES);
+    jmodifiers = GetJavaMouseModifiers(modifierFlags);
 
 JNF_COCOA_EXIT(env);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/Mouse/MouseModifiersUnitTest/MouseModifiersInKeyEvent.java	Tue Dec 29 16:46:11 2015 +0300
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.awt.Component;
+import java.awt.EventQueue;
+import java.awt.MouseInfo;
+import java.awt.Rectangle;
+import java.awt.Robot;
+import java.awt.event.InputEvent;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+
+import javax.swing.JFrame;
+import javax.swing.JTextField;
+
+/**
+ * @test
+ * @bug 8143054
+ */
+public final class MouseModifiersInKeyEvent {
+
+    private static int modifiersEX = 0;
+    private static int modifiers = 0;
+    private static JFrame f;
+    private static Rectangle bounds;
+
+    public static void main(final String[] args) throws Exception {
+        for (int i = 1; i <= MouseInfo.getNumberOfButtons(); ++i) {
+            test(InputEvent.getMaskForButton(i));
+        }
+    }
+
+    private static void test(final int mask) throws Exception {
+        final Robot r = new Robot();
+        r.setAutoDelay(100);
+        r.setAutoWaitForIdle(true);
+
+        EventQueue.invokeAndWait(MouseModifiersInKeyEvent::createAndShowGUI);
+        r.waitForIdle();
+        EventQueue.invokeAndWait(() -> bounds = f.getBounds());
+
+        r.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
+        r.mousePress(mask);
+        r.keyPress(KeyEvent.VK_A);
+        r.keyRelease(KeyEvent.VK_A);
+
+        EventQueue.invokeAndWait(() -> f.dispose());
+
+        r.mouseRelease(mask);
+
+        // all extended modifiers should work
+        if (modifiersEX != mask) {
+            System.err.println("Expected modifiersEX: " + mask);
+            System.err.println("Actual modifiersEX: " + modifiersEX);
+            throw new RuntimeException();
+        }
+        // old modifiers work only for button1
+        if (modifiersEX == InputEvent.BUTTON1_DOWN_MASK) {
+            if (modifiers != InputEvent.BUTTON1_MASK) {
+                System.err.println("Expected modifiers: " + InputEvent.BUTTON1_MASK);
+                System.err.println("Actual modifiers: " + modifiers);
+                throw new RuntimeException();
+            }
+        }
+        modifiersEX = 0;
+        modifiers = 0;
+    }
+
+    private static void createAndShowGUI() {
+        f = new JFrame();
+
+        final Component component = new JTextField();
+        component.addKeyListener(new MyKeyListener());
+
+        f.add(component);
+        f.setSize(300, 300);
+        f.setLocationRelativeTo(null);
+        f.setAlwaysOnTop(true);
+        f.setVisible(true);
+    }
+
+    static final class MyKeyListener extends KeyAdapter {
+
+        public void keyPressed(final KeyEvent e) {
+            modifiersEX = e.getModifiersEx();
+            modifiers = e.getModifiers();
+        }
+    }
+}
+