1 /*
   2  * Copyright (c) 2011, 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 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 8025126
  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         robot.setAutoDelay(50);
  48 
  49         Frame frame = new Frame();
  50         frame.setSize(300, 300);
  51 
  52         frame.addKeyListener(new KeyAdapter() {
  53 
  54             @Override
  55             public void keyPressed(KeyEvent e) {
  56                 eventsCount++;
  57                 setExtendedKeyCode = setExtendedKeyCode && (e.getExtendedKeyCode()
  58                         == ExtendedKeyCodes.getExtendedKeyCodeForChar(e.getKeyChar()));
  59             }
  60 
  61             @Override
  62             public void keyReleased(KeyEvent e) {
  63                 eventsCount++;
  64                 setExtendedKeyCode = setExtendedKeyCode && (e.getExtendedKeyCode()
  65                         == ExtendedKeyCodes.getExtendedKeyCodeForChar(e.getKeyChar()));
  66             }
  67         });
  68 
  69         frame.setVisible(true);
  70         toolkit.realSync();
  71 
  72         robot.keyPress(KeyEvent.VK_D);
  73         robot.keyRelease(KeyEvent.VK_D);
  74         toolkit.realSync();
  75 
  76         frame.dispose();
  77 
  78         if (eventsCount != 2 || !setExtendedKeyCode) {
  79             throw new RuntimeException("Wrong extended key code");
  80         }
  81 
  82         frame = new Frame();
  83         frame.setSize(300, 300);
  84         setExtendedKeyCode = false;
  85 
  86         frame.addKeyListener(new KeyAdapter() {
  87 
  88             @Override
  89             public void keyPressed(KeyEvent e) {
  90                 setExtendedKeyCode = e.getExtendedKeyCode() == KeyEvent.VK_LEFT;
  91             }
  92         });
  93 
  94         frame.setVisible(true);
  95         toolkit.realSync();
  96 
  97         robot.keyPress(KeyEvent.VK_LEFT);
  98         robot.keyRelease(KeyEvent.VK_LEFT);
  99         toolkit.realSync();
 100         frame.dispose();
 101 
 102         if (!setExtendedKeyCode) {
 103             throw new RuntimeException("Wrong extended key code!");
 104         }
 105     }
 106 }