1 /* 2 * Copyright (c) 2012, 2013, 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 /* 25 * @test 26 * @bug 8020209 27 * @summary [macosx] Mac OS X key event confusion for "COMMAND PLUS" 28 * @author leonid.romanov@oracle.com 29 * @run main bug8020209 30 */ 31 32 import sun.awt.*; 33 import java.awt.*; 34 import java.awt.event.*; 35 36 public class bug8020209 { 37 static volatile int listenerCallCounter = 0; 38 39 static AWTKeyStroke keyStrokes[] = { 40 AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_EQUALS, InputEvent.META_MASK), 41 AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_ESCAPE, InputEvent.CTRL_MASK), 42 }; 43 44 public static void main(String[] args) throws Exception { 45 if (sun.awt.OSInfo.getOSType() != sun.awt.OSInfo.OSType.MACOSX) { 46 System.out.println("This test is for MacOS only. Automatically passed on other platforms."); 47 return; 48 } 49 50 System.setProperty("apple.laf.useScreenMenuBar", "true"); 51 52 createAndShowGUI(); 53 54 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 55 Robot robot = new Robot(); 56 robot.setAutoDelay(100); 57 58 for (int i = 0; i < keyStrokes.length; ++i) { 59 AWTKeyStroke ks = keyStrokes[i]; 60 61 int modKeyCode = getModKeyCode(ks.getModifiers()); 62 robot.keyPress(modKeyCode); 63 64 robot.keyPress(ks.getKeyCode()); 65 robot.keyRelease(ks.getKeyCode()); 66 67 robot.keyRelease(modKeyCode); 68 69 toolkit.realSync(); 70 71 if (listenerCallCounter != 4) { 72 throw new Exception("Test failed: KeyListener for '" + ks.toString() + 73 "' called " + listenerCallCounter + " times instead of 4!"); 74 } 75 76 listenerCallCounter = 0; 77 } 78 79 } 80 81 private static void createAndShowGUI() { 82 Frame frame = new Frame("Test"); 83 frame.addKeyListener(new KeyAdapter() { 84 @Override 85 public void keyPressed(KeyEvent e) { 86 listenerCallCounter++; 87 } 88 89 @Override 90 public void keyReleased(KeyEvent e) { 91 listenerCallCounter++; 92 } 93 }); 94 95 frame.pack(); 96 frame.setVisible(true); 97 } 98 99 private static int getModKeyCode(int mod) { 100 if ((mod & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) { 101 return KeyEvent.VK_SHIFT; 102 } 103 104 if ((mod & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) { 105 return KeyEvent.VK_CONTROL; 106 } 107 108 if ((mod & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) { 109 return KeyEvent.VK_ALT; 110 } 111 112 if ((mod & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) { 113 return KeyEvent.VK_META; 114 } 115 116 return 0; 117 } 118 }