1 /* 2 * Copyright (c) 2011, 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.Frame; 25 import java.awt.Robot; 26 import java.awt.Toolkit; 27 import java.awt.event.KeyEvent; 28 import java.awt.event.KeyAdapter; 29 import sun.awt.ExtendedKeyCodes; 30 import sun.awt.SunToolkit; 31 32 /* 33 * @test 34 * @bug 8007156 35 * @summary Extended key code is not set for a key event 36 * @author Alexandr Scherbatiy 37 * @run main ExtendedKeyCodeTest 38 */ 39 public class ExtendedKeyCodeTest { 40 41 private static volatile boolean setExtendedKeyCode = true; 42 private static volatile int eventsCount = 0; 43 44 public static void main(String[] args) throws Exception { 45 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 46 Robot robot = new Robot(); 47 48 Frame frame = new Frame(); 49 frame.setSize(300, 300); 50 51 frame.addKeyListener(new KeyAdapter() { 52 53 @Override 54 public void keyPressed(KeyEvent e) { 55 eventsCount++; 56 setExtendedKeyCode = setExtendedKeyCode && (e.getExtendedKeyCode() 57 == ExtendedKeyCodes.getExtendedKeyCodeForChar(e.getKeyChar())); 58 } 59 60 @Override 61 public void keyReleased(KeyEvent e) { 62 eventsCount++; 63 setExtendedKeyCode = setExtendedKeyCode && (e.getExtendedKeyCode() 64 == ExtendedKeyCodes.getExtendedKeyCodeForChar(e.getKeyChar())); 65 } 66 }); 67 68 frame.setVisible(true); 69 toolkit.realSync(); 70 71 robot.keyPress(KeyEvent.VK_D); 72 robot.keyRelease(KeyEvent.VK_D); 73 toolkit.realSync(); 74 75 frame.dispose(); 76 77 if (eventsCount != 2 || !setExtendedKeyCode) { 78 throw new RuntimeException("Wrong extended key code"); 79 } 80 } 81 }