1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.awt.Component; 25 import java.awt.EventQueue; 26 import java.awt.MouseInfo; 27 import java.awt.Rectangle; 28 import java.awt.Robot; 29 import java.awt.event.InputEvent; 30 import java.awt.event.KeyAdapter; 31 import java.awt.event.KeyEvent; 32 33 import javax.swing.JFrame; 34 import javax.swing.JTextField; 35 36 /** 37 * @test 38 * @bug 8143054 39 */ 40 public final class MouseModifiersInKeyEvent { 41 42 private static int modifiersEX = 0; 43 private static int modifiers = 0; 44 private static JFrame f; 45 private static Rectangle bounds; 46 47 public static void main(final String[] args) throws Exception { 48 for (int i = 1; i <= MouseInfo.getNumberOfButtons(); ++i) { 49 test(InputEvent.getMaskForButton(i)); 50 } 51 } 52 53 private static void test(final int mask) throws Exception { 54 final Robot r = new Robot(); 55 r.setAutoDelay(100); 56 r.setAutoWaitForIdle(true); 57 58 EventQueue.invokeAndWait(MouseModifiersInKeyEvent::createAndShowGUI); 59 r.waitForIdle(); 60 EventQueue.invokeAndWait(() -> bounds = f.getBounds()); 61 62 r.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2); 63 r.mousePress(mask); 64 r.keyPress(KeyEvent.VK_A); 65 r.keyRelease(KeyEvent.VK_A); 66 67 EventQueue.invokeAndWait(() -> f.dispose()); 68 69 r.mouseRelease(mask); 70 71 // all extended modifiers should work 72 if ((modifiersEX & mask) == 0) { 73 System.err.println("Expected modifiersEX: " + mask); 74 System.err.println("Actual modifiersEX: " + modifiersEX); 75 throw new RuntimeException(); 76 } 77 // old modifiers work only for button1 78 if ((modifiersEX & InputEvent.BUTTON1_DOWN_MASK) != 0) { 79 if ((modifiers & InputEvent.BUTTON1_MASK) == 0) { 80 System.err.println("Expected modifiers: " + InputEvent.BUTTON1_MASK); 81 System.err.println("Actual modifiers: " + modifiers); 82 throw new RuntimeException(); 83 } 84 } 85 modifiersEX = 0; 86 modifiers = 0; 87 } 88 89 private static void createAndShowGUI() { 90 f = new JFrame(); 91 92 final Component component = new JTextField(); 93 component.addKeyListener(new MyKeyListener()); 94 95 f.add(component); 96 f.setSize(300, 300); 97 f.setLocationRelativeTo(null); 98 f.setAlwaysOnTop(true); 99 f.setVisible(true); 100 } 101 102 static final class MyKeyListener extends KeyAdapter { 103 104 public void keyPressed(final KeyEvent e) { 105 modifiersEX = e.getModifiersEx(); 106 modifiers = e.getModifiers(); 107 } 108 } 109 } 110