undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
123
test/jdk/java/awt/event/KeyEvent/8020209/bug8020209.java
Normal file
123
test/jdk/java/awt/event/KeyEvent/8020209/bug8020209.java
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2018, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 8020209
|
||||
* @summary [macosx] Mac OS X key event confusion for "COMMAND PLUS"
|
||||
* @author leonid.romanov@oracle.com
|
||||
* @library /test/lib
|
||||
* @build jdk.test.lib.Platform
|
||||
* @run main bug8020209
|
||||
*/
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
import jdk.test.lib.Platform;
|
||||
|
||||
public class bug8020209 {
|
||||
static volatile int listenerCallCounter = 0;
|
||||
|
||||
static AWTKeyStroke keyStrokes[] = {
|
||||
AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_DECIMAL, InputEvent.META_MASK),
|
||||
AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_EQUALS, InputEvent.META_MASK),
|
||||
AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_ESCAPE, InputEvent.CTRL_MASK),
|
||||
};
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (!Platform.isOSX()) {
|
||||
System.out.println("This test is for MacOS only. Automatically passed on other platforms.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.setProperty("apple.laf.useScreenMenuBar", "true");
|
||||
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoDelay(50);
|
||||
|
||||
createAndShowGUI();
|
||||
robot.waitForIdle();
|
||||
|
||||
for (int i = 0; i < keyStrokes.length; ++i) {
|
||||
AWTKeyStroke ks = keyStrokes[i];
|
||||
|
||||
int modKeyCode = getModKeyCode(ks.getModifiers());
|
||||
robot.keyPress(modKeyCode);
|
||||
|
||||
robot.keyPress(ks.getKeyCode());
|
||||
robot.keyRelease(ks.getKeyCode());
|
||||
|
||||
robot.keyRelease(modKeyCode);
|
||||
|
||||
robot.waitForIdle();
|
||||
|
||||
if (listenerCallCounter != 4) {
|
||||
throw new Exception("Test failed: KeyListener for '" + ks.toString() +
|
||||
"' called " + listenerCallCounter + " times instead of 4!");
|
||||
}
|
||||
|
||||
listenerCallCounter = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void createAndShowGUI() {
|
||||
Frame frame = new Frame("Test");
|
||||
frame.addKeyListener(new KeyAdapter() {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
listenerCallCounter++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
listenerCallCounter++;
|
||||
}
|
||||
});
|
||||
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
private static int getModKeyCode(int mod) {
|
||||
if ((mod & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
|
||||
return KeyEvent.VK_SHIFT;
|
||||
}
|
||||
|
||||
if ((mod & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
|
||||
return KeyEvent.VK_CONTROL;
|
||||
}
|
||||
|
||||
if ((mod & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
|
||||
return KeyEvent.VK_ALT;
|
||||
}
|
||||
|
||||
if ((mod & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
|
||||
return KeyEvent.VK_META;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,191 @@
|
|||
/*
|
||||
* Copyright (c) 2009, 2024, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6680988
|
||||
* @key headful
|
||||
* @summary verify that various shortcuts and accelerators work
|
||||
* @library /java/awt/regtesthelpers
|
||||
* @build PassFailJFrame
|
||||
* @run main/manual AcceleratorTest
|
||||
*/
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.InputEvent;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.Hashtable;
|
||||
import javax.swing.AbstractAction;
|
||||
import javax.swing.InputMap;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JMenu;
|
||||
import javax.swing.JMenuBar;
|
||||
import javax.swing.JMenuItem;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.KeyStroke;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
public class AcceleratorTest {
|
||||
static JFrame output;
|
||||
static JTextArea text;
|
||||
static JFrame jfr;
|
||||
static Hashtable<String, Integer> cmdHash = new Hashtable<String, Integer>();
|
||||
static String[] CMD = {
|
||||
"\u042E, keep me in focus",
|
||||
"Item Ctrl Be",
|
||||
"Item Ctrl English Period",
|
||||
"Item Ctrl English N",
|
||||
"\u0436"
|
||||
};
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String instructions =
|
||||
"Ensure you have Russian keyboard layout as a currently active.\n" +
|
||||
"(1) Press Ctrl + \u0411 (a key with \",<\" on it) \n" +
|
||||
"(2) Find a . (period) in this layout (perhaps \"/?\" or \"7&\" key). " +
|
||||
"Press Ctrl + .\n" +
|
||||
"(3) Press Ctrl + regular English . (period) key (on \".>\" )\n" +
|
||||
"(4) Press Ctrl + key with English N.\n" +
|
||||
"(5) Press Alt + \u042E (key with \".>\")\n" +
|
||||
"(6) Press Alt + \u0436 (key with \";:\")\n" +
|
||||
"If all expected commands will be fired, look for message\n" +
|
||||
"\"All tests passed\"";
|
||||
|
||||
for (int i = 0; i < CMD.length; i++) {
|
||||
cmdHash.put(CMD[i], 0);
|
||||
}
|
||||
|
||||
PassFailJFrame testFrame = PassFailJFrame.builder()
|
||||
.title("Test Instructions Frame")
|
||||
.instructions(instructions)
|
||||
.testTimeOut(10)
|
||||
.rows(10)
|
||||
.columns(45)
|
||||
.build();
|
||||
|
||||
SwingUtilities.invokeAndWait(() -> {
|
||||
output = new JFrame("output");
|
||||
text = new JTextArea();
|
||||
output.getContentPane().add(text);
|
||||
|
||||
jfr = new JFrame("AcceleratorTest");
|
||||
jfr.setLayout(new BorderLayout());
|
||||
JButton jbu;
|
||||
jfr.add((jbu = new JButton(CMD[0])));
|
||||
jbu.setMnemonic(java.awt.event.KeyEvent.getExtendedKeyCodeForChar('\u042E'));
|
||||
jbu.addActionListener(new ALi(CMD[0]));
|
||||
|
||||
JMenuBar menuBar = new JMenuBar();
|
||||
jfr.setJMenuBar(menuBar);
|
||||
JMenu menu = new JMenu("Menu");
|
||||
menuBar.add(menu);
|
||||
|
||||
JMenuItem menuItem = new JMenuItem(CMD[1]);
|
||||
menuItem.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent
|
||||
.getExtendedKeyCodeForChar('\u0431'), InputEvent.CTRL_DOWN_MASK));
|
||||
|
||||
JMenuItem menuItemEnglish = new JMenuItem(CMD[2]);
|
||||
menuItemEnglish.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD,
|
||||
InputEvent.CTRL_DOWN_MASK));
|
||||
|
||||
JMenuItem menuItemE1 = new JMenuItem(CMD[3]);
|
||||
menuItemE1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
|
||||
InputEvent.CTRL_DOWN_MASK));
|
||||
|
||||
menuItem.addActionListener(new ALi(CMD[1]));
|
||||
menuItemEnglish.addActionListener(new ALi(CMD[2]));
|
||||
menuItemE1.addActionListener(new ALi(CMD[3]));
|
||||
|
||||
menu.add(menuItem);
|
||||
menu.add(menuItemEnglish);
|
||||
menu.add(menuItemE1);
|
||||
|
||||
KeyStroke ks;
|
||||
InputMap im = new InputMap();
|
||||
ks = KeyStroke.getKeyStroke(KeyEvent.getExtendedKeyCodeForChar('\u0436'),
|
||||
java.awt.event.InputEvent.ALT_DOWN_MASK);
|
||||
im.put(ks, "pushAction");
|
||||
im.setParent(jbu.getInputMap(JComponent.WHEN_FOCUSED));
|
||||
jbu.setInputMap(JComponent.WHEN_FOCUSED, im);
|
||||
|
||||
jbu.getActionMap().put("pushAction",
|
||||
new AbstractAction("pushAction") {
|
||||
public void actionPerformed(ActionEvent evt) {
|
||||
if (evt.getActionCommand().equals(CMD[4])) {
|
||||
cmdHash.put(CMD[4], 1);
|
||||
}
|
||||
boolean notYet = false;
|
||||
for (int i = 0; i < CMD.length; i++) {
|
||||
if (cmdHash.get(CMD[i]) == 0) notYet = true;
|
||||
}
|
||||
text.append(evt.getActionCommand() + " FIRED\n");
|
||||
if (!notYet) {
|
||||
text.append("All tests passed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
testFrame.addTestWindow(jfr);
|
||||
testFrame.addTestWindow(output);
|
||||
|
||||
PassFailJFrame.positionTestWindow(jfr, PassFailJFrame.Position.HORIZONTAL);
|
||||
jfr.setSize(200, 200);
|
||||
|
||||
PassFailJFrame.positionTestWindow(output, PassFailJFrame.Position.HORIZONTAL);
|
||||
output.setSize(200, 200);
|
||||
|
||||
jfr.setVisible(true);
|
||||
output.setVisible(true);
|
||||
|
||||
testFrame.awaitAndCheck();
|
||||
}
|
||||
|
||||
public static class ALi implements ActionListener {
|
||||
String expectedCmd;
|
||||
|
||||
public ALi(String eCmd) {
|
||||
expectedCmd = eCmd;
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent ae) {
|
||||
if (cmdHash.containsKey(ae.getActionCommand())) {
|
||||
cmdHash.put(expectedCmd, 1);
|
||||
}
|
||||
boolean notYet = false;
|
||||
for (int i = 0; i < CMD.length; i++) {
|
||||
if (cmdHash.get(CMD[i]) == 0) notYet = true;
|
||||
//text.append(CMD[i]+":"+cmdHash.get(CMD[i]));
|
||||
}
|
||||
text.append(ae.getActionCommand() + " FIRED\n");
|
||||
if (!notYet) {
|
||||
text.append("All tests passed.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,135 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 8068283
|
||||
* @summary Checks that <Alt>+Char accelerators work when pressed in a text component
|
||||
* @author Anton Nashatyrev
|
||||
* @modules java.desktop/sun.awt
|
||||
* @run main AltCharAcceleratorTest
|
||||
*/
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class AltCharAcceleratorTest {
|
||||
|
||||
boolean action1 = false;
|
||||
boolean action2 = false;
|
||||
|
||||
CountDownLatch focusLatch = new CountDownLatch(1);
|
||||
CountDownLatch actionLatch = new CountDownLatch(2);
|
||||
|
||||
public AltCharAcceleratorTest() throws Exception {
|
||||
SwingUtilities.invokeAndWait(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
JFrame f = new JFrame("frame");
|
||||
final JTextField t = new JTextField();
|
||||
JMenuBar mb = new JMenuBar();
|
||||
JMenu m1 = new JMenu("File");
|
||||
JMenuItem i1 = new JMenuItem("Save");
|
||||
JMenuItem i2 = new JMenuItem("Load");
|
||||
|
||||
i1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, KeyEvent.ALT_MASK));
|
||||
i2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.ALT_MASK));
|
||||
|
||||
i1.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.out.println("Action1!");
|
||||
action1 = true;
|
||||
actionLatch.countDown();
|
||||
}
|
||||
});
|
||||
|
||||
i2.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.out.println("Action2!");
|
||||
action2 = true;
|
||||
actionLatch.countDown();
|
||||
}
|
||||
});
|
||||
|
||||
t.addFocusListener(new FocusAdapter() {
|
||||
@Override
|
||||
public void focusGained(FocusEvent e) {
|
||||
System.out.println("Focused!");
|
||||
focusLatch.countDown();
|
||||
}
|
||||
});
|
||||
|
||||
t.setColumns(10);
|
||||
t.requestFocusInWindow();
|
||||
|
||||
f.setJMenuBar(mb);
|
||||
mb.add(m1);
|
||||
m1.add(i1);
|
||||
m1.add(i2);
|
||||
|
||||
f.setLayout(new FlowLayout());
|
||||
f.add(t);
|
||||
f.setSize(200, 200);
|
||||
|
||||
f.setVisible(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void test() throws Exception {
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoDelay(100);
|
||||
robot.waitForIdle();
|
||||
|
||||
focusLatch.await(5, TimeUnit.SECONDS);
|
||||
|
||||
robot.keyPress(KeyEvent.VK_ALT);
|
||||
robot.keyPress(KeyEvent.VK_T);
|
||||
robot.keyRelease(KeyEvent.VK_T);
|
||||
robot.keyRelease(KeyEvent.VK_ALT);
|
||||
|
||||
robot.keyPress(KeyEvent.VK_ALT);
|
||||
robot.keyPress(KeyEvent.VK_F);
|
||||
robot.keyRelease(KeyEvent.VK_F);
|
||||
robot.keyRelease(KeyEvent.VK_ALT);
|
||||
|
||||
actionLatch.await(5, TimeUnit.SECONDS);
|
||||
|
||||
if (!action1 || !action2) {
|
||||
throw new RuntimeException("Actions not performed");
|
||||
}
|
||||
|
||||
System.out.println("Passed.");
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
AltCharAcceleratorTest t = new AltCharAcceleratorTest();
|
||||
t.test();
|
||||
}
|
||||
}
|
||||
90
test/jdk/java/awt/event/KeyEvent/AltGrTest.java
Normal file
90
test/jdk/java/awt/event/KeyEvent/AltGrTest.java
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4122687 4209844
|
||||
* @summary Characters typed with AltGr have Alt bit set on
|
||||
* KEY_TYPED events
|
||||
* @requires (os.family == "windows")
|
||||
* @library /java/awt/regtesthelpers
|
||||
* @build PassFailJFrame
|
||||
* @run main/manual AltGrTest
|
||||
*/
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Frame;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class AltGrTest extends Frame implements KeyListener {
|
||||
static String INSTRUCTIONS = """
|
||||
Switch to German (Germany) keyboard layout and type
|
||||
few characters using <AltGr> key.
|
||||
Note: on windows keyboards without an AltGr key,
|
||||
you should use Ctrl-Alt to synthesize AltGr.
|
||||
For example, on German keyboards, `@' is AltGr-Q
|
||||
`{' is AltGr-7 and '[' is AltGr-8
|
||||
If you see the corresponding symbols appear in the text field
|
||||
and there are no entries in log area starting with word "FAIL:"
|
||||
press "Pass", otherwise press "Fail".
|
||||
""";
|
||||
|
||||
public AltGrTest() {
|
||||
setLayout(new BorderLayout());
|
||||
TextField entry = new TextField();
|
||||
entry.addKeyListener(this);
|
||||
add(entry, BorderLayout.CENTER);
|
||||
pack();
|
||||
}
|
||||
|
||||
public void keyTyped(KeyEvent e) {
|
||||
PassFailJFrame.log("----");
|
||||
PassFailJFrame.log("Got " + e);
|
||||
|
||||
if (e.isControlDown() || e.isAltDown()) {
|
||||
PassFailJFrame.log("FAIL: character typed has following modifiers bits set:");
|
||||
PassFailJFrame.log((e.isControlDown() ? " Control" : "")
|
||||
+ (e.isAltDown() ? " Alt" : ""));
|
||||
}
|
||||
|
||||
if (!(e.isAltGraphDown())) {
|
||||
PassFailJFrame.log("FAIL: AltGraph modifier is missing");
|
||||
}
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent ignore) {}
|
||||
public void keyReleased(KeyEvent ignore) {}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException,
|
||||
InvocationTargetException {
|
||||
PassFailJFrame.builder()
|
||||
.instructions(INSTRUCTIONS)
|
||||
.logArea(10)
|
||||
.testUI(AltGrTest::new)
|
||||
.build()
|
||||
.awaitAndCheck();
|
||||
}
|
||||
}
|
||||
140
test/jdk/java/awt/event/KeyEvent/AltGraphModifier.java
Normal file
140
test/jdk/java/awt/event/KeyEvent/AltGraphModifier.java
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
/*
|
||||
* Copyright (c) 2000, 2023, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
@test
|
||||
@bug 4343344
|
||||
@summary Tests key modifiers when ALT_GRAPH key is pressed by Robot.
|
||||
@key headful
|
||||
@requires (os.family != "mac")
|
||||
@run main AltGraphModifier
|
||||
*/
|
||||
|
||||
import java.awt.AWTException;
|
||||
import java.awt.Color;
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.Frame;
|
||||
import java.awt.Robot;
|
||||
import java.awt.event.InputEvent;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
|
||||
public class AltGraphModifier {
|
||||
static Frame frame;
|
||||
|
||||
static final int[] modifierKeys = {
|
||||
KeyEvent.VK_ALT_GRAPH
|
||||
};
|
||||
|
||||
static final int[] inputMasks = {
|
||||
InputEvent.ALT_GRAPH_DOWN_MASK
|
||||
};
|
||||
|
||||
static boolean[] modifierPress = new boolean[modifierKeys.length];
|
||||
|
||||
static volatile boolean modKeys;
|
||||
static int modKeyCount;
|
||||
static volatile boolean failed = false;
|
||||
|
||||
|
||||
public static void main (String args[]) throws
|
||||
InterruptedException, InvocationTargetException, AWTException {
|
||||
|
||||
EventQueue.invokeAndWait(() -> {
|
||||
frame = new Frame("Modifier Robot Key BUG");
|
||||
frame.setLayout(new FlowLayout());
|
||||
frame.setSize(200, 200);
|
||||
frame.addKeyListener(new KeyListener() {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent kp){
|
||||
System.out.println(kp);
|
||||
if (modKeys == true) {
|
||||
for (int i=0; i < modifierKeys.length; i++) {
|
||||
if (modifierPress[i] == true) {
|
||||
if ((kp.getModifiersEx() & inputMasks[i]) == inputMasks[i]) {
|
||||
} else {
|
||||
failed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent kr){
|
||||
}
|
||||
});
|
||||
frame.setBackground(Color.blue);
|
||||
frame.setVisible(true);
|
||||
});
|
||||
|
||||
try {
|
||||
Robot robot = new Robot();
|
||||
|
||||
robot.delay(1000);
|
||||
robot.mouseMove((int) (frame.getLocationOnScreen().getX()
|
||||
+ frame.getWidth() / 2),
|
||||
(int) (frame.getLocationOnScreen().getY()
|
||||
+ frame.getHeight() / 2));
|
||||
robot.delay(1000);
|
||||
robot.setAutoDelay(1000);
|
||||
|
||||
//Imbed operations here
|
||||
modKeys = true;
|
||||
|
||||
for (modKeyCount = 0; modKeyCount < modifierKeys.length; modKeyCount++) {
|
||||
//Press the Modifier Key
|
||||
modifierPress[modKeyCount] = true;
|
||||
robot.keyPress(modifierKeys[modKeyCount]);
|
||||
|
||||
frame.requestFocus();
|
||||
robot.delay(1000);
|
||||
|
||||
//Press the Modifier Key
|
||||
modifierPress[modKeyCount] = false;
|
||||
robot.keyRelease(modifierKeys[modKeyCount]);
|
||||
robot.delay(1000);
|
||||
}
|
||||
} finally {
|
||||
EventQueue.invokeAndWait(() -> {
|
||||
if (frame != null) {
|
||||
frame.dispose();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (failed) {
|
||||
throw new RuntimeException("FAIL MESSAGE ---- Modifier "
|
||||
+" Mask is not set when the Key : "
|
||||
+"AltGraph"
|
||||
+ " Key is pressed by Robot.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
124
test/jdk/java/awt/event/KeyEvent/CRTest.java
Normal file
124
test/jdk/java/awt/event/KeyEvent/CRTest.java
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4257434
|
||||
* @summary Ensures that the right results are produced by the
|
||||
* carriage return keys.
|
||||
* @library /java/awt/regtesthelpers
|
||||
* @build PassFailJFrame
|
||||
* @run main/manual CRTest
|
||||
*/
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Button;
|
||||
import java.awt.Frame;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class CRTest extends Frame implements KeyListener, ActionListener {
|
||||
StringBuilder error = new StringBuilder();
|
||||
AtomicBoolean actionCompleted = new AtomicBoolean(false);
|
||||
static String INSTRUCTIONS = """
|
||||
This test requires keyboard with the numeric keypad (numpad).
|
||||
If your keyboard does not have numpad press "Pass" to skip testing.
|
||||
Click on the text field in window named "Check KeyChar values".
|
||||
Press Enter on keypad. Then press Return key on a standard keyboard.
|
||||
Then click on "Done" button. Test will pass or fail automatically.
|
||||
""";
|
||||
|
||||
public CRTest() {
|
||||
super("Check KeyChar values");
|
||||
setLayout(new BorderLayout());
|
||||
TextField tf = new TextField(30);
|
||||
|
||||
tf.addKeyListener(this);
|
||||
tf.addActionListener(this);
|
||||
|
||||
add(tf, BorderLayout.CENTER);
|
||||
|
||||
Button done = new Button("Done");
|
||||
done.addActionListener((event) -> {
|
||||
checkAndComplete();
|
||||
});
|
||||
add(done, BorderLayout.SOUTH);
|
||||
pack();
|
||||
}
|
||||
|
||||
public void checkAndComplete() {
|
||||
if (!actionCompleted.get()) {
|
||||
error.append("\nNo action received!");
|
||||
}
|
||||
|
||||
if (!error.isEmpty()) {
|
||||
PassFailJFrame.forceFail(error.toString());
|
||||
} else {
|
||||
PassFailJFrame.forcePass();
|
||||
}
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent evt) {
|
||||
if ((evt.getKeyChar() != '\n') || (evt.getKeyCode() != KeyEvent.VK_ENTER)) {
|
||||
error.append("\nKeyPressed: Unexpected code " + evt.getKeyCode());
|
||||
} else {
|
||||
PassFailJFrame.log("KeyPressed Test PASSED");
|
||||
}
|
||||
}
|
||||
|
||||
public void keyTyped(KeyEvent evt) {
|
||||
if ((evt.getKeyChar() != '\n') || (evt.getKeyCode() != KeyEvent.VK_UNDEFINED)) {
|
||||
error.append("\nKeyTyped: Unexpected code " + evt.getKeyCode());
|
||||
} else {
|
||||
PassFailJFrame.log("KeyTyped Test PASSED");
|
||||
}
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent evt) {
|
||||
if ((evt.getKeyChar() != '\n') || (evt.getKeyCode() != KeyEvent.VK_ENTER)) {
|
||||
error.append("\nKeyReleased: Unexpected code " + evt.getKeyCode());
|
||||
} else {
|
||||
PassFailJFrame.log("KeyReleased Test PASSED");
|
||||
}
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent evt) {
|
||||
PassFailJFrame.log("ActionPerformed Test PASSED");
|
||||
actionCompleted.set(true);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException,
|
||||
InvocationTargetException {
|
||||
PassFailJFrame.builder()
|
||||
.instructions(INSTRUCTIONS)
|
||||
.logArea(10)
|
||||
.testUI(CRTest::new)
|
||||
.build()
|
||||
.awaitAndCheck();
|
||||
}
|
||||
}
|
||||
95
test/jdk/java/awt/event/KeyEvent/CharUndefinedTest.java
Normal file
95
test/jdk/java/awt/event/KeyEvent/CharUndefinedTest.java
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4115484 4164672 4167893
|
||||
* @summary Ensures that KeyEvent has right keyChar for modifier and action keys.
|
||||
* @library /java/awt/regtesthelpers
|
||||
* @build PassFailJFrame
|
||||
* @run main/manual CharUndefinedTest
|
||||
*/
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Frame;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class CharUndefinedTest extends Frame implements KeyListener {
|
||||
|
||||
static String INSTRUCTIONS = """
|
||||
Click on the text field inside the window named "Check KeyChar values".
|
||||
Of any of the keys mentioned in this list that exist on your keyboard
|
||||
press each of the listed keys once and also press them in two-key combinations such as
|
||||
Control-Shift or Alt-Control.
|
||||
The list of keys is: "Control, Shift, Meta, Alt, Command, Option".
|
||||
After that press all function keys from F1 to F12 once,
|
||||
Insert, Home, End, PageUp, PageDown and four arrow keys.
|
||||
Check the log area below. If there are no messages starting with word "ERROR"
|
||||
press "Pass" otherwise press "Fail".
|
||||
""";
|
||||
|
||||
public CharUndefinedTest() {
|
||||
super("Check KeyChar values");
|
||||
setLayout(new BorderLayout());
|
||||
TextField tf = new TextField(30);
|
||||
tf.addKeyListener(this);
|
||||
add(tf, BorderLayout.CENTER);
|
||||
pack();
|
||||
tf.requestFocus();
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
|
||||
PassFailJFrame.log("ERROR: KeyPressed: keyChar = " + e.getKeyChar() +
|
||||
" keyCode = " + e.getKeyCode() + " " + e.getKeyText(e.getKeyCode()));
|
||||
}
|
||||
}
|
||||
|
||||
public void keyTyped(KeyEvent e) {
|
||||
if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
|
||||
PassFailJFrame.log("ERROR: KeyTyped: keyChar = " + e.getKeyChar() +
|
||||
" keyCode = " + e.getKeyCode() + " " + e.getKeyText(e.getKeyCode()));
|
||||
}
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
|
||||
PassFailJFrame.log("ERROR: KeyReleased: keyChar = " + e.getKeyChar() +
|
||||
" keyCode = " + e.getKeyCode() + " " + e.getKeyText(e.getKeyCode()));
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException,
|
||||
InvocationTargetException {
|
||||
PassFailJFrame.builder()
|
||||
.instructions(INSTRUCTIONS)
|
||||
.columns(45)
|
||||
.logArea(10)
|
||||
.testUI(CharUndefinedTest::new)
|
||||
.build()
|
||||
.awaitAndCheck();
|
||||
}
|
||||
}
|
||||
142
test/jdk/java/awt/event/KeyEvent/CorrectTime/CorrectTime.java
Normal file
142
test/jdk/java/awt/event/KeyEvent/CorrectTime/CorrectTime.java
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
/*
|
||||
* Copyright (c) 2007, 2018, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
@test
|
||||
@key headful
|
||||
@bug 6451578
|
||||
@library ../../../regtesthelpers
|
||||
@build Sysout AbstractTest Util
|
||||
@summary A key event could have wrong time
|
||||
@author andrei dmitriev : area=awt.event
|
||||
@run main CorrectTime
|
||||
*/
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import test.java.awt.regtesthelpers.AbstractTest;
|
||||
import test.java.awt.regtesthelpers.Sysout;
|
||||
import test.java.awt.regtesthelpers.Util;
|
||||
|
||||
public class CorrectTime extends Frame implements KeyListener {
|
||||
//Maximum time the event should arrive to listener
|
||||
final static int REASONABLE_PATH_TIME = 5000;
|
||||
static TextField tf = new TextField("press keys", 10);
|
||||
static TextArea ta = new TextArea("press keys", 10, 10);
|
||||
static Button bt = new Button("press button");
|
||||
static List list = new List();
|
||||
final static Robot robot = Util.createRobot();
|
||||
|
||||
public CorrectTime(){
|
||||
super("Check time of KeyEvents");
|
||||
|
||||
setPreferredSize(new Dimension(400,400));
|
||||
setLayout(new FlowLayout());
|
||||
|
||||
list.add("item1");
|
||||
list.add("item2");
|
||||
list.add("item3");
|
||||
|
||||
add(bt);
|
||||
add(tf);
|
||||
add(ta);
|
||||
add(list);
|
||||
|
||||
bt.addKeyListener(this);
|
||||
tf.addKeyListener(this);
|
||||
ta.addKeyListener(this);
|
||||
list.addKeyListener(this);
|
||||
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
public static void main(String []s) {
|
||||
Frame frame = new CorrectTime();
|
||||
|
||||
Robot robot = Util.createRobot();
|
||||
Util.waitForIdle(robot);
|
||||
|
||||
testComponent(tf);
|
||||
testComponent(ta);
|
||||
testComponent(bt);
|
||||
testComponent(list);
|
||||
|
||||
}
|
||||
|
||||
private static void testComponent(final Component comp){
|
||||
Runnable action = new Runnable(){
|
||||
public void run(){
|
||||
Util.clickOnComp(comp, robot);
|
||||
Util.waitForIdle(robot);
|
||||
}
|
||||
};
|
||||
|
||||
if (! Util.trackFocusGained(comp, action, REASONABLE_PATH_TIME, true)){
|
||||
AbstractTest.fail("Focus didn't come to " + comp);
|
||||
}
|
||||
testKeys();
|
||||
Util.waitForIdle(robot);
|
||||
}
|
||||
|
||||
private static void testKeys(){
|
||||
//set of keys is random
|
||||
typeKey(KeyEvent.VK_A);
|
||||
typeKey(KeyEvent.VK_B);
|
||||
typeKey(KeyEvent.VK_SPACE);
|
||||
typeKey(KeyEvent.VK_Z);
|
||||
}
|
||||
|
||||
private static void typeKey(int keyChar){
|
||||
try {
|
||||
robot.keyPress(keyChar);
|
||||
robot.delay(5);
|
||||
} finally {
|
||||
robot.keyRelease(keyChar);
|
||||
}
|
||||
robot.delay(100);
|
||||
}
|
||||
|
||||
private void traceKey(String k, KeyEvent e){
|
||||
long eventTime = e.getWhen();
|
||||
long currTime = System.currentTimeMillis();
|
||||
long diff = currTime - eventTime;
|
||||
System.out.println(k + " diff is " + diff + ", event is "+ e);
|
||||
if (diff < 0 ||
|
||||
diff > REASONABLE_PATH_TIME)
|
||||
{
|
||||
AbstractTest.fail(k + " diff is " + diff + ", event = "+e);
|
||||
}
|
||||
}
|
||||
|
||||
public void keyTyped(KeyEvent e){
|
||||
traceKey("keytyped",e);
|
||||
}
|
||||
public void keyPressed(KeyEvent e){
|
||||
traceKey("keypress",e);
|
||||
}
|
||||
public void keyReleased(KeyEvent e){
|
||||
traceKey("keyrelease",e);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,133 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2018, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 7199180
|
||||
* @summary [macosx] Dead keys handling for input methods
|
||||
* @author alexandr.scherbatiy area=awt.event
|
||||
* @library /test/lib
|
||||
* @build jdk.test.lib.Platform
|
||||
* @run main DeadKeyMacOSXInputText
|
||||
*/
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import jdk.test.lib.Platform;
|
||||
|
||||
public class DeadKeyMacOSXInputText {
|
||||
|
||||
private static volatile int state = 0;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
if (!Platform.isOSX()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoDelay(50);
|
||||
|
||||
createAndShowGUI(robot);
|
||||
|
||||
// Pressed keys: Alt + E + A
|
||||
// Results: ALT + VK_DEAD_ACUTE + a with accute accent
|
||||
robot.keyPress(KeyEvent.VK_ALT);
|
||||
robot.keyPress(KeyEvent.VK_E);
|
||||
robot.keyRelease(KeyEvent.VK_E);
|
||||
robot.keyRelease(KeyEvent.VK_ALT);
|
||||
|
||||
robot.keyPress(KeyEvent.VK_A);
|
||||
robot.keyRelease(KeyEvent.VK_A);
|
||||
robot.waitForIdle();
|
||||
|
||||
if (state != 3) {
|
||||
throw new RuntimeException("Wrong number of key events.");
|
||||
}
|
||||
}
|
||||
|
||||
static void createAndShowGUI(Robot robot) {
|
||||
Frame frame = new Frame();
|
||||
frame.setSize(300, 300);
|
||||
Panel panel = new Panel(new BorderLayout());
|
||||
JTextField textField = new JTextField();
|
||||
textField.addKeyListener(new DeadKeyListener());
|
||||
panel.add(textField, BorderLayout.CENTER);
|
||||
frame.add(panel);
|
||||
frame.setVisible(true);
|
||||
robot.waitForIdle();
|
||||
|
||||
textField.requestFocusInWindow();
|
||||
robot.waitForIdle();
|
||||
|
||||
}
|
||||
|
||||
static class DeadKeyListener extends KeyAdapter {
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
int keyCode = e.getKeyCode();
|
||||
char keyChar = e.getKeyChar();
|
||||
|
||||
switch (state) {
|
||||
case 0:
|
||||
if (keyCode != KeyEvent.VK_ALT) {
|
||||
throw new RuntimeException("Alt is not pressed.");
|
||||
}
|
||||
state++;
|
||||
break;
|
||||
case 1:
|
||||
if (keyCode != KeyEvent.VK_DEAD_ACUTE) {
|
||||
throw new RuntimeException("Dead ACUTE is not pressed.");
|
||||
}
|
||||
if (keyChar != 0xB4) {
|
||||
throw new RuntimeException("Pressed char is not dead acute.");
|
||||
}
|
||||
state++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
int keyCode = e.getKeyCode();
|
||||
char keyChar = e.getKeyChar();
|
||||
|
||||
if (state == 2) {
|
||||
if (keyCode != 0) {
|
||||
throw new RuntimeException("Key code should be undefined.");
|
||||
}
|
||||
if (keyChar != 0xE1) {
|
||||
throw new RuntimeException("A char does not have ACCUTE accent");
|
||||
}
|
||||
state++;
|
||||
} else {
|
||||
throw new RuntimeException("Wron number of keyTyped events.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2016, 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.Frame;
|
||||
import java.awt.Robot;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.KeyEvent;
|
||||
/*
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 8013849
|
||||
* @summary Awt assert on Hashtable.cpp:124
|
||||
* @author alexandr.scherbatiy area=awt.event
|
||||
* @run main/timeout=5 DeadKeySystemAssertionDialog
|
||||
*/
|
||||
|
||||
public class DeadKeySystemAssertionDialog {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
Frame frame = new Frame();
|
||||
frame.setSize(300, 200);
|
||||
|
||||
TextField textField = new TextField();
|
||||
frame.add(textField);
|
||||
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoDelay(50);
|
||||
|
||||
frame.setVisible(true);
|
||||
robot.waitForIdle();
|
||||
|
||||
textField.requestFocus();
|
||||
robot.waitForIdle();
|
||||
|
||||
// Check that the system assertion dialog does not block Java
|
||||
robot.keyPress(KeyEvent.VK_A);
|
||||
robot.keyRelease(KeyEvent.VK_A);
|
||||
robot.waitForIdle();
|
||||
|
||||
frame.setVisible(false);
|
||||
frame.dispose();
|
||||
}
|
||||
}
|
||||
139
test/jdk/java/awt/event/KeyEvent/DeadKey/deadKeyMacOSX.java
Normal file
139
test/jdk/java/awt/event/KeyEvent/DeadKey/deadKeyMacOSX.java
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2018, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 7196547
|
||||
* @summary Dead Key implementation for KeyEvent on Mac OS X
|
||||
* @author alexandr.scherbatiy area=awt.event
|
||||
* @library /test/lib
|
||||
* @build jdk.test.lib.Platform
|
||||
* @run main deadKeyMacOSX
|
||||
*/
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
import jdk.test.lib.Platform;
|
||||
|
||||
public class deadKeyMacOSX {
|
||||
|
||||
private static volatile int state = 0;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
if (!Platform.isOSX()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoDelay(50);
|
||||
|
||||
createAndShowGUI(robot);
|
||||
|
||||
// Pressed keys: Alt + E + A
|
||||
// Results: ALT + VK_DEAD_ACUTE + a with accute accent
|
||||
robot.keyPress(KeyEvent.VK_ALT);
|
||||
robot.keyPress(KeyEvent.VK_E);
|
||||
robot.keyRelease(KeyEvent.VK_E);
|
||||
robot.keyRelease(KeyEvent.VK_ALT);
|
||||
|
||||
robot.keyPress(KeyEvent.VK_A);
|
||||
robot.keyRelease(KeyEvent.VK_A);
|
||||
|
||||
if (state != 3) {
|
||||
throw new RuntimeException("Wrong number of key events.");
|
||||
}
|
||||
}
|
||||
|
||||
static void createAndShowGUI(Robot robot) {
|
||||
Frame frame = new Frame();
|
||||
frame.setSize(300, 300);
|
||||
Panel panel = new Panel();
|
||||
panel.addKeyListener(new DeadKeyListener());
|
||||
frame.add(panel);
|
||||
frame.setVisible(true);
|
||||
robot.waitForIdle();
|
||||
|
||||
panel.requestFocusInWindow();
|
||||
robot.waitForIdle();
|
||||
}
|
||||
|
||||
static class DeadKeyListener extends KeyAdapter {
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
int keyCode = e.getKeyCode();
|
||||
char keyChar = e.getKeyChar();
|
||||
|
||||
switch (state) {
|
||||
case 0:
|
||||
if (keyCode != KeyEvent.VK_ALT) {
|
||||
throw new RuntimeException("Alt is not pressed.");
|
||||
}
|
||||
state++;
|
||||
break;
|
||||
case 1:
|
||||
if (keyCode != KeyEvent.VK_DEAD_ACUTE) {
|
||||
throw new RuntimeException("Dead ACUTE is not pressed.");
|
||||
}
|
||||
if (keyChar != 0xB4) {
|
||||
throw new RuntimeException("Pressed char is not dead acute.");
|
||||
}
|
||||
|
||||
state++;
|
||||
break;
|
||||
case 2:
|
||||
if (keyCode != KeyEvent.VK_A) {
|
||||
throw new RuntimeException("A is not pressed.");
|
||||
}
|
||||
if (keyChar != 0xE1) {
|
||||
throw new RuntimeException("A char does not have ACCUTE accent");
|
||||
}
|
||||
state++;
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException("Excessive keyPressed event.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
int keyCode = e.getKeyCode();
|
||||
char keyChar = e.getKeyChar();
|
||||
|
||||
if (state == 3) {
|
||||
if (keyCode != 0) {
|
||||
throw new RuntimeException("Key code should be undefined.");
|
||||
}
|
||||
if (keyChar != 0xE1) {
|
||||
throw new RuntimeException("A char does not have ACCUTE accent");
|
||||
}
|
||||
} else {
|
||||
throw new RuntimeException("Wron number of keyTyped events.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,145 @@
|
|||
/*
|
||||
* Copyright (c) 2020, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
@test
|
||||
@bug 6348035
|
||||
@summary F10 should bring down menu even if keystroke target is disabled
|
||||
@requires (os.family == "linux")
|
||||
@key headful
|
||||
@run main DisabledTargetF10
|
||||
*/
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
public class DisabledTargetF10 implements ActionListener {
|
||||
Button b1 = new Button("Disable");
|
||||
Button b3 = new Button("Dummy");
|
||||
TextField b2 = new TextField("Click Me");
|
||||
Button b4 = new Button("Dummy, too");
|
||||
Panel p = new Panel(new GridLayout(1,3));
|
||||
MenuItem exit = new MenuItem ("exit");
|
||||
Frame frame;
|
||||
Robot robot = null;
|
||||
boolean success = false;
|
||||
public static final int MAX_COUNT = 100;
|
||||
|
||||
public void init() {
|
||||
frame = new Frame();
|
||||
frame.setSize(300,300);
|
||||
|
||||
Menu file = new Menu ("file");
|
||||
exit.setShortcut(new MenuShortcut(KeyEvent.VK_SPACE, false));
|
||||
exit.addActionListener(this);
|
||||
MenuBar mb = new MenuBar();
|
||||
mb.add(file);
|
||||
file.add(exit);
|
||||
frame.setMenuBar(mb);
|
||||
|
||||
p.add(b1);
|
||||
p.add(b2);
|
||||
p.add(b3);
|
||||
frame.add("North", b4);
|
||||
frame.add("South", p);
|
||||
b1.addActionListener(this);
|
||||
}
|
||||
|
||||
public void start () {
|
||||
boolean isX = Toolkit.getDefaultToolkit().getClass().getName().equals("sun.awt.X11.XToolkit");
|
||||
frame.setLocation(300,300);
|
||||
frame.setVisible( true );
|
||||
try {
|
||||
robot = new Robot();
|
||||
}catch(AWTException e) {
|
||||
throw new RuntimeException("unable to create robot", e);
|
||||
}
|
||||
robot.delay(1000); // wait for frames visible
|
||||
Point pt;
|
||||
frame.toFront();
|
||||
pt = getLocation(b1);
|
||||
robot.delay(200);
|
||||
int x, y;
|
||||
|
||||
x = pt.x + b1.getWidth() / 2;
|
||||
y = pt.y + b1.getHeight() / 2;
|
||||
robot.mouseMove(x,y);
|
||||
robot.mousePress(InputEvent.BUTTON1_MASK);
|
||||
robot.delay(200);
|
||||
robot.mouseRelease( InputEvent.BUTTON1_MASK );
|
||||
robot.delay(200);
|
||||
|
||||
robot.keyPress(KeyEvent.VK_F10);
|
||||
robot.delay(40);
|
||||
robot.keyRelease(KeyEvent.VK_F10);
|
||||
robot.delay(1000);
|
||||
robot.keyPress(KeyEvent.VK_ENTER);
|
||||
robot.delay(40);
|
||||
robot.keyRelease(KeyEvent.VK_ENTER);
|
||||
if(!isX) {
|
||||
robot.delay(200);
|
||||
robot.keyPress(KeyEvent.VK_ENTER);
|
||||
robot.delay(40);
|
||||
robot.keyRelease(KeyEvent.VK_ENTER);
|
||||
}
|
||||
robot.delay(200);
|
||||
if(!success) {
|
||||
throw new RuntimeException("Menu was not brought down by F10, apparently.");
|
||||
}
|
||||
}
|
||||
|
||||
public static Point getLocation(Component co) throws RuntimeException {
|
||||
Point pt = null;
|
||||
boolean bFound = false;
|
||||
int count = 0;
|
||||
while( !bFound ) {
|
||||
try {
|
||||
pt = co.getLocationOnScreen();
|
||||
bFound = true;
|
||||
} catch(Exception ex) {
|
||||
bFound = false;
|
||||
count++;
|
||||
}
|
||||
if(!bFound && count > MAX_COUNT) {
|
||||
throw new RuntimeException("don't see a component to get location");
|
||||
}
|
||||
}
|
||||
return pt;
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent evt) {
|
||||
if (evt.getSource() == b1) {
|
||||
b1.setEnabled(false);
|
||||
b2.setEnabled(false);
|
||||
b3.setEnabled(false);
|
||||
}else if(evt.getSource() == exit) {
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
DisabledTargetF10 imt = new DisabledTargetF10();
|
||||
imt.init();
|
||||
imt.start();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2025, 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.Frame;
|
||||
import java.awt.Robot;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyAdapter;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 8007156 8025126
|
||||
* @summary Tests that ExtendedKeyCode is set for key events
|
||||
* @library /lib/client
|
||||
* @build ExtendedRobot
|
||||
* @run main ExtendedKeyCodeTest
|
||||
*/
|
||||
public class ExtendedKeyCodeTest {
|
||||
|
||||
private static volatile boolean setExtendedKeyCode = true;
|
||||
private static volatile int eventsCount = 0;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ExtendedRobot robot = new ExtendedRobot();
|
||||
robot.setAutoDelay(50);
|
||||
|
||||
Frame frame = new Frame();
|
||||
frame.setSize(300, 300);
|
||||
|
||||
frame.addKeyListener(new KeyAdapter() {
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
eventsCount++;
|
||||
setExtendedKeyCode = setExtendedKeyCode && (e.getExtendedKeyCode()
|
||||
== KeyEvent.getExtendedKeyCodeForChar(e.getKeyChar()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
eventsCount++;
|
||||
setExtendedKeyCode = setExtendedKeyCode && (e.getExtendedKeyCode()
|
||||
== KeyEvent.getExtendedKeyCodeForChar(e.getKeyChar()));
|
||||
}
|
||||
});
|
||||
|
||||
frame.setVisible(true);
|
||||
robot.waitForIdle();
|
||||
|
||||
robot.keyPress(KeyEvent.VK_D);
|
||||
robot.keyRelease(KeyEvent.VK_D);
|
||||
robot.waitForIdle();
|
||||
|
||||
frame.dispose();
|
||||
|
||||
if (eventsCount != 2 || !setExtendedKeyCode) {
|
||||
throw new RuntimeException("Wrong extended key code\n" +
|
||||
"eventsCount: " + eventsCount);
|
||||
}
|
||||
|
||||
frame = new Frame();
|
||||
frame.setSize(300, 300);
|
||||
setExtendedKeyCode = false;
|
||||
|
||||
frame.addKeyListener(new KeyAdapter() {
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
setExtendedKeyCode = e.getExtendedKeyCode() == KeyEvent.VK_LEFT;
|
||||
}
|
||||
});
|
||||
|
||||
frame.setVisible(true);
|
||||
robot.waitForIdle();
|
||||
|
||||
robot.keyPress(KeyEvent.VK_LEFT);
|
||||
robot.keyRelease(KeyEvent.VK_LEFT);
|
||||
robot.waitForIdle();
|
||||
frame.dispose();
|
||||
|
||||
if (!setExtendedKeyCode) {
|
||||
throw new RuntimeException("Wrong extended key code!");
|
||||
}
|
||||
}
|
||||
}
|
||||
66
test/jdk/java/awt/event/KeyEvent/ExtendedKeysTest.java
Normal file
66
test/jdk/java/awt/event/KeyEvent/ExtendedKeysTest.java
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4218892 4191924 4199284
|
||||
* @summary Unable to enter some chars via european keyboard layout(s)
|
||||
* @library /java/awt/regtesthelpers
|
||||
* @build PassFailJFrame
|
||||
* @run main/manual ExtendedKeysTest
|
||||
*/
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Frame;
|
||||
import java.awt.TextArea;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class ExtendedKeysTest extends Frame {
|
||||
static String INSTRUCTIONS = """
|
||||
This test requires Swiss German input. If the Swiss German input
|
||||
can not be installed or configured press "Pass" to skip testing.
|
||||
Click on the text area inside the window named "Check input".
|
||||
Switch to Swiss German input and press key with "\\" on it
|
||||
(usually this key is above or to the left of the main "Enter" key).
|
||||
If you see a dollar sign press "Pass".
|
||||
If you see any other character or question mark press "Fail".
|
||||
""";
|
||||
|
||||
public ExtendedKeysTest() {
|
||||
super("Check input");
|
||||
setLayout(new BorderLayout());
|
||||
add(new TextArea(20, 20), "Center");
|
||||
pack();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException,
|
||||
InvocationTargetException {
|
||||
PassFailJFrame.builder()
|
||||
.instructions(INSTRUCTIONS)
|
||||
.columns(45)
|
||||
.logArea(10)
|
||||
.testUI(ExtendedKeysTest::new)
|
||||
.build()
|
||||
.awaitAndCheck();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,287 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2022, 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.
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 8043126 8145116
|
||||
* @summary Check whether
|
||||
* 1. correct extended modifiers are returned
|
||||
* by KeyEvent.getModifiersEx()
|
||||
* 2. InputEvent.getModifiersExText() returns
|
||||
* correct extended modifier keys description
|
||||
*
|
||||
* @library /lib/client/ ../../helpers/lwcomponents/
|
||||
* @library /test/lib
|
||||
* @build LWComponent
|
||||
* @build LWButton
|
||||
* @build LWList
|
||||
* @build ExtendedRobot
|
||||
* @run main/timeout=300 ExtendedModifiersTest
|
||||
*/
|
||||
import java.awt.Button;
|
||||
import java.awt.Color;
|
||||
import java.awt.Component;
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.Frame;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.List;
|
||||
import java.awt.Point;
|
||||
import java.awt.TextArea;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.InputEvent;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static jdk.test.lib.Asserts.*;
|
||||
import test.java.awt.event.helpers.lwcomponents.LWButton;
|
||||
import test.java.awt.event.helpers.lwcomponents.LWList;
|
||||
|
||||
public class ExtendedModifiersTest implements KeyListener {
|
||||
|
||||
Frame frame;
|
||||
Button button;
|
||||
LWButton buttonLW;
|
||||
TextField textField;
|
||||
TextArea textArea;
|
||||
List list;
|
||||
LWList listLW;
|
||||
|
||||
private final ExtendedRobot robot;
|
||||
private static final int WAIT_DELAY = 5000;
|
||||
private static final int KEY_DELAY = 100;
|
||||
private final Object lock;
|
||||
|
||||
private boolean keyPressedFlag;
|
||||
private int modifiersEx = 0;
|
||||
private String exText = "";
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
|
||||
return;
|
||||
}
|
||||
modifiersEx = e.getModifiersEx();
|
||||
exText = InputEvent.getModifiersExText(modifiersEx);
|
||||
keyPressedFlag = true;
|
||||
|
||||
synchronized (lock) {
|
||||
lock.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
}
|
||||
|
||||
public void createGUI() {
|
||||
|
||||
frame = new Frame();
|
||||
frame.setTitle("ExtendedModifiersTest");
|
||||
frame.setLayout(new GridLayout(1, 6));
|
||||
frame.setLocationRelativeTo(null);
|
||||
|
||||
button = new Button();
|
||||
button.addKeyListener(this);
|
||||
frame.add(button);
|
||||
|
||||
buttonLW = new LWButton();
|
||||
buttonLW.addKeyListener(this);
|
||||
frame.add(buttonLW);
|
||||
|
||||
textField = new TextField(5);
|
||||
textField.addKeyListener(this);
|
||||
frame.add(textField);
|
||||
|
||||
textArea = new TextArea(5, 5);
|
||||
textArea.addKeyListener(this);
|
||||
frame.add(textArea);
|
||||
|
||||
list = new List();
|
||||
for (int i = 1; i <= 5; ++i) {
|
||||
list.add("item " + i);
|
||||
}
|
||||
list.addKeyListener(this);
|
||||
frame.add(list);
|
||||
|
||||
listLW = new LWList();
|
||||
for (int i = 1; i <= 5; ++i) {
|
||||
listLW.add("item " + i);
|
||||
}
|
||||
listLW.addKeyListener(this);
|
||||
frame.add(listLW);
|
||||
|
||||
frame.setBackground(Color.gray);
|
||||
frame.setSize(500, 100);
|
||||
frame.setVisible(true);
|
||||
frame.toFront();
|
||||
}
|
||||
|
||||
public ExtendedModifiersTest() throws Exception {
|
||||
lock = new Object();
|
||||
robot = new ExtendedRobot();
|
||||
EventQueue.invokeAndWait(this::createGUI);
|
||||
}
|
||||
|
||||
private void runScenario(int keys[], int expectedMask) {
|
||||
if (keys.length < 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int k = 0; k < keys.length; ++k) {
|
||||
|
||||
keyPressedFlag = false;
|
||||
robot.keyPress(keys[k]);
|
||||
robot.delay(KEY_DELAY);
|
||||
|
||||
if (!keyPressedFlag) {
|
||||
synchronized (lock) {
|
||||
try {
|
||||
lock.wait(WAIT_DELAY);
|
||||
} catch (InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!keyPressedFlag) {
|
||||
robot.keyRelease(keys[k]);
|
||||
robot.delay(KEY_DELAY);
|
||||
assertTrue(false, "key press event was not received");
|
||||
}
|
||||
}
|
||||
|
||||
int modEx = modifiersEx & expectedMask;
|
||||
|
||||
for (int k = keys.length - 1; k >= 0; --k) {
|
||||
robot.keyRelease(keys[k]);
|
||||
robot.delay(KEY_DELAY);
|
||||
}
|
||||
|
||||
assertEQ(expectedMask, modEx, "invalid extended modifiers");
|
||||
|
||||
for (int k = 0; k < keys.length; ++k) {
|
||||
String keyText = KeyEvent.getKeyText(keys[k]).toLowerCase();
|
||||
assertTrue(exText.toLowerCase().contains(keyText),
|
||||
"invalid extended modifier keys description");
|
||||
}
|
||||
|
||||
System.out.println(exText + " : passed");
|
||||
|
||||
robot.type(KeyEvent.VK_ESCAPE);
|
||||
robot.waitForIdle();
|
||||
}
|
||||
|
||||
private void doTest() throws Exception {
|
||||
|
||||
ArrayList<Component> components = new ArrayList();
|
||||
components.add(button);
|
||||
components.add(buttonLW);
|
||||
components.add(textField);
|
||||
components.add(textArea);
|
||||
components.add(list);
|
||||
components.add(listLW);
|
||||
|
||||
String OS = System.getProperty("os.name").toLowerCase();
|
||||
System.out.println(OS);
|
||||
|
||||
for (Component c : components) {
|
||||
|
||||
String className = c.getClass().getName();
|
||||
System.out.println("component class : " + className);
|
||||
|
||||
Point origin = c.getLocationOnScreen();
|
||||
int xc = origin.x + c.getWidth() / 2;
|
||||
int yc = origin.y + c.getHeight() / 2;
|
||||
Point center = new Point(xc, yc);
|
||||
|
||||
robot.waitForIdle();
|
||||
robot.glide(origin, center);
|
||||
robot.click();
|
||||
robot.waitForIdle();
|
||||
|
||||
// 1. shift + control
|
||||
runScenario(new int[]{KeyEvent.VK_SHIFT, KeyEvent.VK_CONTROL},
|
||||
InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK);
|
||||
|
||||
// 2. alt + shift + control
|
||||
runScenario(new int[]{KeyEvent.VK_ALT, KeyEvent.VK_SHIFT,
|
||||
KeyEvent.VK_CONTROL}, InputEvent.ALT_DOWN_MASK
|
||||
| InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK);
|
||||
|
||||
// 3. shift
|
||||
runScenario(new int[]{KeyEvent.VK_SHIFT},
|
||||
InputEvent.SHIFT_DOWN_MASK);
|
||||
|
||||
// 4. alt + control
|
||||
runScenario(new int[]{KeyEvent.VK_ALT, KeyEvent.VK_CONTROL},
|
||||
InputEvent.ALT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK);
|
||||
|
||||
// 5. shift + alt
|
||||
runScenario(new int[]{KeyEvent.VK_SHIFT, KeyEvent.VK_ALT},
|
||||
InputEvent.SHIFT_DOWN_MASK | InputEvent.ALT_DOWN_MASK);
|
||||
|
||||
if (OS.contains("os x")) {
|
||||
// 6. meta
|
||||
runScenario(new int[]{KeyEvent.VK_META},
|
||||
InputEvent.META_DOWN_MASK);
|
||||
|
||||
// 7. shift + ctrl + alt + meta
|
||||
runScenario(new int[]{KeyEvent.VK_SHIFT, KeyEvent.VK_CONTROL,
|
||||
KeyEvent.VK_ALT, KeyEvent.VK_META},
|
||||
InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK
|
||||
| InputEvent.ALT_DOWN_MASK | InputEvent.META_DOWN_MASK);
|
||||
|
||||
// 8. meta + shift + ctrl
|
||||
runScenario(new int[]{KeyEvent.VK_META, KeyEvent.VK_SHIFT,
|
||||
KeyEvent.VK_CONTROL}, InputEvent.META_DOWN_MASK
|
||||
| InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK);
|
||||
|
||||
// 9. meta + shift + alt
|
||||
runScenario(new int[]{KeyEvent.VK_META, KeyEvent.VK_SHIFT,
|
||||
KeyEvent.VK_ALT}, InputEvent.META_DOWN_MASK
|
||||
| InputEvent.SHIFT_DOWN_MASK | InputEvent.ALT_DOWN_MASK);
|
||||
|
||||
// 10. meta + ctrl + alt
|
||||
runScenario(new int[]{KeyEvent.VK_META, KeyEvent.VK_CONTROL,
|
||||
KeyEvent.VK_ALT}, InputEvent.META_DOWN_MASK
|
||||
| InputEvent.CTRL_DOWN_MASK | InputEvent.ALT_DOWN_MASK);
|
||||
}
|
||||
}
|
||||
|
||||
robot.waitForIdle();
|
||||
frame.dispose();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ExtendedModifiersTest test = new ExtendedModifiersTest();
|
||||
test.doTest();
|
||||
}
|
||||
}
|
||||
71
test/jdk/java/awt/event/KeyEvent/FrenchKeyboard.java
Normal file
71
test/jdk/java/awt/event/KeyEvent/FrenchKeyboard.java
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* Copyright (c) 2000, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4308606
|
||||
* @summary Tests whether the keys on the numeric keyboard work
|
||||
* correctly under French input locale.
|
||||
* @key i18n
|
||||
* @library /java/awt/regtesthelpers
|
||||
* @build PassFailJFrame
|
||||
* @run main/manual FrenchKeyboard
|
||||
*/
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Frame;
|
||||
import java.awt.TextField;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class FrenchKeyboard extends Frame {
|
||||
static String INSTRUCTIONS = """
|
||||
This test is intended for computers with French input method. If French
|
||||
input method can not be enabled or your keyboard does not have a numeric
|
||||
keypad press "Pass" to skip the test.
|
||||
Make sure that French input method is active and the NumLock is on.
|
||||
Click on the text field in the window called "Check your keys"
|
||||
and type once of each of the following keys on the numeric keypad:
|
||||
/*-+1234567890
|
||||
If all the expected characters are displayed exactly once press "Pass".
|
||||
If any characters do not display or display multiple times press "Fail".
|
||||
""";
|
||||
|
||||
public FrenchKeyboard() {
|
||||
super("Check your keys");
|
||||
setLayout(new BorderLayout());
|
||||
TextField tf = new TextField(30);
|
||||
add(tf, BorderLayout.CENTER);
|
||||
pack();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException,
|
||||
InvocationTargetException {
|
||||
PassFailJFrame.builder()
|
||||
.title("FrenchKeyboard Instructions")
|
||||
.instructions(INSTRUCTIONS)
|
||||
.testUI(FrenchKeyboard::new)
|
||||
.build()
|
||||
.awaitAndCheck();
|
||||
}
|
||||
}
|
||||
|
||||
183
test/jdk/java/awt/event/KeyEvent/FunctionKeyTest.java
Normal file
183
test/jdk/java/awt/event/KeyEvent/FunctionKeyTest.java
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2024, 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.BorderLayout;
|
||||
import java.awt.Button;
|
||||
import java.awt.Color;
|
||||
import java.awt.Event;
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.Frame;
|
||||
import java.awt.Label;
|
||||
import java.awt.Robot;
|
||||
import java.awt.TextArea;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static java.awt.Event.KEY_ACTION;
|
||||
import static java.awt.Event.KEY_ACTION_RELEASE;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4011219
|
||||
* @summary Test for function key press/release received by Java client.
|
||||
* @key headful
|
||||
*/
|
||||
|
||||
public final class FunctionKeyTest {
|
||||
private static Frame frame;
|
||||
private static Robot robot;
|
||||
|
||||
private static final CyclicBarrier keyPress = new CyclicBarrier(2);
|
||||
private static final CyclicBarrier keyRelease = new CyclicBarrier(2);
|
||||
|
||||
private static final CountDownLatch frameActivated = new CountDownLatch(1);
|
||||
|
||||
private static final List<Error> failures = new ArrayList<>(4);
|
||||
private static final AtomicReference<Exception> edtException = new AtomicReference<>();
|
||||
|
||||
private static void testKey(int keyCode, String keyText) throws Exception {
|
||||
robot.keyPress(keyCode);
|
||||
try {
|
||||
keyPress.await(2, SECONDS);
|
||||
} catch (TimeoutException e) {
|
||||
keyPress.reset();
|
||||
failures.add(new Error(keyText + " key press is not received", e));
|
||||
}
|
||||
|
||||
robot.keyRelease(keyCode);
|
||||
try {
|
||||
keyRelease.await(2, SECONDS);
|
||||
} catch (TimeoutException e) {
|
||||
keyRelease.reset();
|
||||
failures.add(new Error(keyText + " key release is not received", e));
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
robot = new Robot();
|
||||
robot.setAutoDelay(150);
|
||||
|
||||
try {
|
||||
EventQueue.invokeAndWait(() -> {
|
||||
frame = new FunctionKeyTester();
|
||||
frame.setSize(200, 200);
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowActivated(WindowEvent e) {
|
||||
System.out.println("frame.windowActivated");
|
||||
frameActivated.countDown();
|
||||
}
|
||||
});
|
||||
frame.setVisible(true);
|
||||
});
|
||||
|
||||
if (!frameActivated.await(2, SECONDS)) {
|
||||
throw new Error("Frame wasn't activated");
|
||||
}
|
||||
robot.delay(100);
|
||||
|
||||
testKey(KeyEvent.VK_F11, "F11");
|
||||
testKey(KeyEvent.VK_F12, "F12");
|
||||
} finally {
|
||||
EventQueue.invokeAndWait(() -> {
|
||||
if (frame != null) {
|
||||
frame.dispose();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (!failures.isEmpty()) {
|
||||
System.err.println("Failures detected:");
|
||||
failures.forEach(System.err::println);
|
||||
if (edtException.get() != null) {
|
||||
System.err.println("\nException on EDT:");
|
||||
edtException.get().printStackTrace();
|
||||
}
|
||||
System.err.println();
|
||||
throw new RuntimeException("Test failed: " + failures.get(0).getMessage(),
|
||||
failures.get(0));
|
||||
}
|
||||
|
||||
if (edtException.get() != null) {
|
||||
throw new RuntimeException("Test failed because of exception on EDT",
|
||||
edtException.get());
|
||||
}
|
||||
}
|
||||
|
||||
private static final class FunctionKeyTester extends Frame {
|
||||
Label l = new Label ("NULL");
|
||||
Button b = new Button("button");
|
||||
TextArea log = new TextArea();
|
||||
|
||||
FunctionKeyTester() {
|
||||
super("Function Key Test");
|
||||
this.setLayout(new BorderLayout());
|
||||
this.add(BorderLayout.NORTH, l);
|
||||
this.add(BorderLayout.SOUTH, b);
|
||||
this.add(BorderLayout.CENTER, log);
|
||||
log.setFocusable(false);
|
||||
log.setEditable(false);
|
||||
l.setBackground(Color.red);
|
||||
setSize(200, 200);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
public boolean handleEvent(Event e) {
|
||||
String message = "e.id=" + e.id + "\n";
|
||||
System.out.print(message);
|
||||
log.append(message);
|
||||
|
||||
try {
|
||||
switch (e.id) {
|
||||
case KEY_ACTION
|
||||
-> keyPress.await();
|
||||
case KEY_ACTION_RELEASE
|
||||
-> keyRelease.await();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
if (!edtException.compareAndSet(null, ex)) {
|
||||
edtException.get().addSuppressed(ex);
|
||||
}
|
||||
}
|
||||
|
||||
return super.handleEvent(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
public boolean keyDown(Event e, int key) {
|
||||
l.setText("e.key=" + e.key);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
126
test/jdk/java/awt/event/KeyEvent/HomeEndKeyTest.java
Normal file
126
test/jdk/java/awt/event/KeyEvent/HomeEndKeyTest.java
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* bug 4268912 4115514
|
||||
* summary Ensures that KeyEvent has right results for the following
|
||||
* non-numpad keys: Home/Eng/PageUp/PageDn
|
||||
* @library /java/awt/regtesthelpers
|
||||
* @build PassFailJFrame
|
||||
* @run main/manual HomeEndKeyTest
|
||||
*/
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Frame;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
|
||||
public class HomeEndKeyTest extends Frame implements KeyListener {
|
||||
static String INSTRUCTIONS = """
|
||||
Before starting this test make sure that system shortcuts and
|
||||
keybindings for the keys in the list below are disabled.
|
||||
For example pressing "Print Screen" key should not launch
|
||||
screen capturing software.
|
||||
Click on the text field in the window named "Check keyCode values"
|
||||
and one by one start typing the keys in the list below.
|
||||
(If you do not have some of the keys on your keyboard skip it
|
||||
and move to the next line).
|
||||
After clicking each key look at the log area - the printed name
|
||||
and key code should correspond to the ones for the key you typed.
|
||||
Note that on some systems the graphical symbol for the key
|
||||
can be printed instead of the symbolic name.
|
||||
If you do not encounter unexpected key codes for the keys you typed,
|
||||
press "Pass". Otherwise press "Fail".
|
||||
|
||||
Key Keycode
|
||||
-------------------------
|
||||
PrintScreen 154
|
||||
ScrollLock 145
|
||||
Pause 19
|
||||
|
||||
Insert 155
|
||||
Del 127
|
||||
Home 36
|
||||
End 35
|
||||
PageUp 33
|
||||
PageDown 34
|
||||
|
||||
Left Arrow 37
|
||||
Up Arrow 38
|
||||
Right Arrow 39
|
||||
Down Arrow 40
|
||||
""";
|
||||
|
||||
public HomeEndKeyTest() {
|
||||
super("Check KeyCode values");
|
||||
setLayout(new BorderLayout());
|
||||
TextField tf = new TextField(30);
|
||||
tf.addKeyListener(this);
|
||||
add(tf, BorderLayout.CENTER);
|
||||
pack();
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
}
|
||||
|
||||
public void keyTyped(KeyEvent ignore) {
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
}
|
||||
|
||||
protected void printKey(KeyEvent evt) {
|
||||
String str;
|
||||
switch (evt.getID()) {
|
||||
case KeyEvent.KEY_PRESSED:
|
||||
str = "KEY_PRESSED";
|
||||
break;
|
||||
case KeyEvent.KEY_RELEASED:
|
||||
str = "KEY_RELEASED";
|
||||
break;
|
||||
default:
|
||||
str = "unknown type";
|
||||
}
|
||||
|
||||
str = str + ":name=" + KeyEvent.getKeyText(evt.getKeyCode()) +
|
||||
" keyCode=" + evt.getKeyCode();
|
||||
PassFailJFrame.log(str);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException,
|
||||
InvocationTargetException {
|
||||
PassFailJFrame.builder()
|
||||
.title("HomeEndKeyTest Instructions")
|
||||
.instructions(INSTRUCTIONS)
|
||||
.logArea(20)
|
||||
.testUI(HomeEndKeyTest::new)
|
||||
.build()
|
||||
.awaitAndCheck();
|
||||
}
|
||||
}
|
||||
81
test/jdk/java/awt/event/KeyEvent/KeyChar/KeyCharTest.java
Normal file
81
test/jdk/java/awt/event/KeyEvent/KeyChar/KeyCharTest.java
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Copyright (c) 2013, 2016, 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.AWTEvent;
|
||||
import java.awt.Frame;
|
||||
import java.awt.Robot;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.event.AWTEventListener;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.Locale;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 8022401 8160623
|
||||
* @summary Wrong key char
|
||||
* @author Alexandr Scherbatiy
|
||||
* @run main KeyCharTest
|
||||
*/
|
||||
public class KeyCharTest {
|
||||
|
||||
private static volatile int eventsCount = 0;
|
||||
|
||||
static {
|
||||
Locale.setDefault(Locale.ENGLISH);
|
||||
|
||||
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
|
||||
|
||||
@Override
|
||||
public void eventDispatched(AWTEvent event) {
|
||||
eventsCount++;
|
||||
char delete = ((KeyEvent) event).getKeyChar();
|
||||
if (delete != '\u007f') {
|
||||
throw new RuntimeException("Key char is not delete: '" + delete + "'");
|
||||
}
|
||||
}
|
||||
}, AWTEvent.KEY_EVENT_MASK);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
|
||||
Frame frame = new Frame();
|
||||
frame.setSize(300, 300);
|
||||
frame.setVisible(true);
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoDelay(50);
|
||||
robot.waitForIdle();
|
||||
|
||||
|
||||
robot.keyPress(KeyEvent.VK_DELETE);
|
||||
robot.keyRelease(KeyEvent.VK_DELETE);
|
||||
robot.waitForIdle();
|
||||
|
||||
frame.dispose();
|
||||
|
||||
if (eventsCount != 3) {
|
||||
throw new RuntimeException("Wrong number of key events: " + eventsCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
116
test/jdk/java/awt/event/KeyEvent/KeyCharTest/KeyCharTest.java
Normal file
116
test/jdk/java/awt/event/KeyEvent/KeyCharTest/KeyCharTest.java
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* Copyright (c) 2004, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
@test
|
||||
@bug 5013984 8360647
|
||||
@summary Tests KEY_PRESSED has the same KeyChar as KEY_RELEASED
|
||||
@key headful
|
||||
@run main KeyCharTest
|
||||
*/
|
||||
|
||||
import java.awt.Frame;
|
||||
import java.awt.Robot;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class KeyCharTest extends Frame implements KeyListener {
|
||||
HashMap<Integer, Character> transMap = new HashMap<>();
|
||||
|
||||
public void keyTyped(KeyEvent e){
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e){
|
||||
transMap.put(e.getKeyCode(), e.getKeyChar());
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e){
|
||||
Character value = transMap.get(e.getKeyCode());
|
||||
if (value != null && e.getKeyChar() != value) {
|
||||
throw new RuntimeException("Wrong KeyChar on KEY_RELEASED "+
|
||||
KeyEvent.getKeyText(e.getKeyCode()));
|
||||
}
|
||||
}
|
||||
|
||||
private void testKeyRange(Robot robot, int start, int end) {
|
||||
System.out.printf("\nTesting range on %d to %d\n", start, end);
|
||||
for(int vkey = start; vkey <= end; vkey++) {
|
||||
try {
|
||||
robot.keyPress(vkey);
|
||||
robot.keyRelease(vkey);
|
||||
System.out.println(KeyEvent.getKeyText(vkey) + " " + vkey);
|
||||
} catch (RuntimeException ignored) {}
|
||||
}
|
||||
robot.delay(100);
|
||||
}
|
||||
|
||||
public void start() throws Exception {
|
||||
Robot robot = new Robot();
|
||||
addKeyListener(this);
|
||||
setLocationRelativeTo(null);
|
||||
setSize(200, 200);
|
||||
setVisible(true);
|
||||
requestFocus();
|
||||
|
||||
boolean wasNumlockPressed = false;
|
||||
try {
|
||||
robot.setAutoDelay(10);
|
||||
robot.setAutoWaitForIdle(true);
|
||||
robot.delay(100);
|
||||
robot.mouseMove(getLocationOnScreen().x + getWidth()/2,
|
||||
getLocationOnScreen().y + getHeight()/2);
|
||||
|
||||
robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
|
||||
robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
|
||||
|
||||
testKeyRange(robot, 0x20, 0x7E);
|
||||
|
||||
// Try again with a different numpad state.
|
||||
robot.keyPress(KeyEvent.VK_NUM_LOCK);
|
||||
robot.keyRelease(KeyEvent.VK_NUM_LOCK);
|
||||
wasNumlockPressed = true;
|
||||
|
||||
testKeyRange(robot, KeyEvent.VK_NUMPAD0, KeyEvent.VK_DIVIDE);
|
||||
} catch(Exception e){
|
||||
throw new RuntimeException("Exception while performing Robot actions.");
|
||||
} finally {
|
||||
if (wasNumlockPressed) {
|
||||
robot.keyPress(KeyEvent.VK_NUM_LOCK);
|
||||
robot.keyRelease(KeyEvent.VK_NUM_LOCK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
KeyCharTest test = new KeyCharTest();
|
||||
try {
|
||||
test.start();
|
||||
} finally {
|
||||
test.setVisible(false);
|
||||
test.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
111
test/jdk/java/awt/event/KeyEvent/KeyDownCaptureTest.java
Normal file
111
test/jdk/java/awt/event/KeyEvent/KeyDownCaptureTest.java
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4093998
|
||||
* @summary keyDown not called on subclasses of Component
|
||||
* @key headful
|
||||
* @run main KeyDownCaptureTest
|
||||
*/
|
||||
|
||||
import java.awt.AWTException;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Canvas;
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.Frame;
|
||||
import java.awt.Point;
|
||||
import java.awt.Robot;
|
||||
import java.awt.event.InputEvent;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class KeyDownCaptureTest extends Frame implements KeyListener {
|
||||
static AtomicBoolean passed = new AtomicBoolean(false);
|
||||
|
||||
public KeyDownCaptureTest() {
|
||||
super("Key Down Capture Test");
|
||||
}
|
||||
|
||||
public void initUI() {
|
||||
setLayout (new BorderLayout());
|
||||
setSize(200, 200);
|
||||
setLocationRelativeTo(null);
|
||||
Canvas canvas = new Canvas();
|
||||
canvas.setBackground(Color.RED);
|
||||
canvas.addKeyListener(this);
|
||||
add(canvas, BorderLayout.CENTER);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
public void middle(Point p) {
|
||||
Point loc = getLocationOnScreen();
|
||||
Dimension size = getSize();
|
||||
p.setLocation(loc.x + (size.width / 2), loc.y + (size.height / 2));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent ignore) {}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
passed.set(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent ignore) {}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException,
|
||||
InvocationTargetException, AWTException {
|
||||
KeyDownCaptureTest test = new KeyDownCaptureTest();
|
||||
try {
|
||||
EventQueue.invokeAndWait((test::initUI));
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoDelay(50);
|
||||
robot.delay(500);
|
||||
robot.waitForIdle();
|
||||
Point target = new Point();
|
||||
EventQueue.invokeAndWait(() -> {
|
||||
test.middle(target);
|
||||
});
|
||||
robot.mouseMove(target.x, target.y);
|
||||
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
|
||||
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
|
||||
robot.keyPress(KeyEvent.VK_SPACE);
|
||||
robot.keyRelease(KeyEvent.VK_SPACE);
|
||||
robot.delay(100);
|
||||
robot.waitForIdle();
|
||||
if (!passed.get()) {
|
||||
throw new RuntimeException("KeyPressed has not arrived to canvas");
|
||||
}
|
||||
} finally {
|
||||
if (test != null) {
|
||||
EventQueue.invokeAndWait(test::dispose);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
212
test/jdk/java/awt/event/KeyEvent/KeyEventLocationTest.java
Normal file
212
test/jdk/java/awt/event/KeyEvent/KeyEventLocationTest.java
Normal file
|
|
@ -0,0 +1,212 @@
|
|||
/*
|
||||
* Copyright (c) 2002, 2022, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 4424517
|
||||
* @summary Verify the mapping of various KeyEvents with their KeyLocations
|
||||
* is as expected.
|
||||
* @run main KeyEventLocationTest
|
||||
*/
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.Frame;
|
||||
import java.awt.Label;
|
||||
import java.awt.Robot;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
public class KeyEventLocationTest {
|
||||
|
||||
private static volatile Frame frame;
|
||||
private static volatile boolean keyPressed;
|
||||
private static volatile boolean keyReleased;
|
||||
private static volatile boolean keyTyped;
|
||||
private static volatile Robot robot;
|
||||
private static volatile int xLocation;
|
||||
private static volatile int yLocation;
|
||||
private static volatile int width;
|
||||
private static volatile int height;
|
||||
private static volatile Label label = new Label();
|
||||
private static volatile String currentString = "";
|
||||
|
||||
private static int[] keyEvents = { KeyEvent.VK_0, KeyEvent.VK_1,
|
||||
KeyEvent.VK_2, KeyEvent.VK_3, KeyEvent.VK_4, KeyEvent.VK_5,
|
||||
KeyEvent.VK_6, KeyEvent.VK_7, KeyEvent.VK_8, KeyEvent.VK_9,
|
||||
KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C, KeyEvent.VK_D,
|
||||
KeyEvent.VK_E, KeyEvent.VK_F, KeyEvent.VK_G, KeyEvent.VK_H,
|
||||
KeyEvent.VK_I, KeyEvent.VK_J, KeyEvent.VK_K, KeyEvent.VK_L,
|
||||
KeyEvent.VK_M, KeyEvent.VK_N, KeyEvent.VK_O, KeyEvent.VK_P,
|
||||
KeyEvent.VK_Q, KeyEvent.VK_R, KeyEvent.VK_S, KeyEvent.VK_T,
|
||||
KeyEvent.VK_U, KeyEvent.VK_V, KeyEvent.VK_W, KeyEvent.VK_X,
|
||||
KeyEvent.VK_Y, KeyEvent.VK_Z, KeyEvent.VK_BACK_QUOTE,
|
||||
KeyEvent.VK_BACK_SLASH, KeyEvent.VK_BACK_SPACE,
|
||||
KeyEvent.VK_CLOSE_BRACKET, KeyEvent.VK_COMMA, KeyEvent.VK_EQUALS,
|
||||
KeyEvent.VK_ESCAPE, KeyEvent.VK_MINUS, KeyEvent.VK_OPEN_BRACKET,
|
||||
KeyEvent.VK_PERIOD, KeyEvent.VK_QUOTE, KeyEvent.VK_SEMICOLON,
|
||||
KeyEvent.VK_SLASH, KeyEvent.VK_SPACE };
|
||||
|
||||
private static int specialKeyEvents[] = { KeyEvent.VK_F1, KeyEvent.VK_F2,
|
||||
KeyEvent.VK_F3, KeyEvent.VK_F4, KeyEvent.VK_F5, KeyEvent.VK_F6,
|
||||
KeyEvent.VK_F7, KeyEvent.VK_F8, KeyEvent.VK_F9, KeyEvent.VK_F10 };
|
||||
|
||||
private static void createGUI() {
|
||||
frame = new Frame("Test frame");
|
||||
frame.setLayout(new BorderLayout());
|
||||
frame.setAlwaysOnTop(true);
|
||||
|
||||
frame.addKeyListener(new KeyListener() {
|
||||
public void keyPressed(KeyEvent event) {
|
||||
try {
|
||||
handleEvent("keyPressed", event);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
keyPressed = true;
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent event) {
|
||||
try {
|
||||
handleEvent("keyReleased", event);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
keyReleased = true;
|
||||
}
|
||||
|
||||
public void keyTyped(KeyEvent event) {
|
||||
try {
|
||||
handleEvent("keyTyped", event);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
keyTyped = true;
|
||||
}
|
||||
|
||||
private void handleEvent(String eventString, KeyEvent event)
|
||||
throws Exception {
|
||||
label.setText(eventString + " triggered for " + event);
|
||||
if ((event.getID() == KeyEvent.KEY_TYPED
|
||||
&& event.getKeyLocation() != KeyEvent.KEY_LOCATION_UNKNOWN)
|
||||
|| ((event.getID() == KeyEvent.KEY_PRESSED
|
||||
|| event.getID() == KeyEvent.KEY_PRESSED)
|
||||
&& event.getKeyLocation()
|
||||
!= KeyEvent.KEY_LOCATION_STANDARD)) {
|
||||
throw new Exception("FAIL: Incorrect KeyLocation: "
|
||||
+ event.getKeyLocation() + " returned when "
|
||||
+ eventString + " triggered for " + event.getKeyChar());
|
||||
}
|
||||
}
|
||||
});
|
||||
label.setText("Current Event: ");
|
||||
frame.add(label, BorderLayout.SOUTH);
|
||||
frame.setSize(600, 300);
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setVisible(true);
|
||||
frame.toFront();
|
||||
}
|
||||
|
||||
private static void doTest() throws Exception {
|
||||
robot = new Robot();
|
||||
robot.setAutoWaitForIdle(true);
|
||||
robot.setAutoDelay(100);
|
||||
|
||||
SwingUtilities.invokeAndWait(() -> {
|
||||
xLocation = frame.getLocationOnScreen().x;
|
||||
yLocation = frame.getLocationOnScreen().y;
|
||||
width = frame.getWidth();
|
||||
height = frame.getHeight();
|
||||
});
|
||||
|
||||
robot.mouseMove(xLocation + width / 2, yLocation + height / 2);
|
||||
robot.mousePress(MouseEvent.BUTTON1_MASK);
|
||||
robot.mouseRelease(MouseEvent.BUTTON1_MASK);
|
||||
|
||||
for (int i = 0; i < keyEvents.length; i++) {
|
||||
resetValues();
|
||||
robot.keyPress(keyEvents[i]);
|
||||
robot.delay(200);
|
||||
if (!keyPressed) {
|
||||
throw new Exception(
|
||||
"FAIL: keyPressed did not get triggered for "
|
||||
+ KeyEvent.getKeyText(keyEvents[i]));
|
||||
}
|
||||
robot.keyRelease(keyEvents[i]);
|
||||
robot.delay(200);
|
||||
if (!keyReleased) {
|
||||
throw new Exception(
|
||||
"FAIL: keyReleased did not get triggered for "
|
||||
+ KeyEvent.getKeyText(keyEvents[i]));
|
||||
}
|
||||
robot.delay(200);
|
||||
if (!keyTyped) {
|
||||
throw new Exception("FAIL: keyTyped did not get triggered for "
|
||||
+ KeyEvent.getKeyText(keyEvents[i]));
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < specialKeyEvents.length; i++) {
|
||||
resetValues();
|
||||
robot.keyPress(specialKeyEvents[i]);
|
||||
robot.delay(200);
|
||||
if (!keyPressed) {
|
||||
throw new Exception("FAIL: keyPressed did not get triggered"
|
||||
+ " for " + KeyEvent.getKeyText(specialKeyEvents[i]));
|
||||
}
|
||||
robot.keyRelease(specialKeyEvents[i]);
|
||||
robot.delay(200);
|
||||
if (!keyReleased) {
|
||||
throw new Exception("FAIL: keyReleased got triggered for "
|
||||
+ KeyEvent.getKeyText(specialKeyEvents[i]));
|
||||
}
|
||||
robot.delay(200);
|
||||
if (keyTyped) {
|
||||
throw new Exception("FAIL: keyTyped got triggered for "
|
||||
+ KeyEvent.getKeyText(specialKeyEvents[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void resetValues() {
|
||||
keyPressed = false;
|
||||
keyReleased = false;
|
||||
keyTyped = false;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
try {
|
||||
EventQueue.invokeAndWait(() -> createGUI());
|
||||
doTest();
|
||||
System.out.println("Test Passed");
|
||||
} finally {
|
||||
if (frame != null)
|
||||
EventQueue.invokeAndWait(() -> frame.dispose());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
125
test/jdk/java/awt/event/KeyEvent/KeyEventToLightweight.java
Normal file
125
test/jdk/java/awt/event/KeyEvent/KeyEventToLightweight.java
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4397557
|
||||
* @summary Check that focused lightweight component gets key events
|
||||
* even if mouse is outside of it or on top of heavyweight component
|
||||
* @key headful
|
||||
* @run main KeyEventToLightweight
|
||||
*/
|
||||
|
||||
import java.awt.AWTException;
|
||||
import java.awt.Button;
|
||||
import java.awt.Component;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.Frame;
|
||||
import java.awt.Point;
|
||||
import java.awt.Robot;
|
||||
import java.awt.event.InputEvent;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import javax.swing.JButton;
|
||||
|
||||
public class KeyEventToLightweight extends Frame {
|
||||
JButton lwbutton = new JButton("Select Me");
|
||||
Button hwbutton = new Button("Heavyweight");
|
||||
|
||||
AtomicBoolean aTyped = new AtomicBoolean(false);
|
||||
AtomicBoolean bTyped = new AtomicBoolean(false);
|
||||
AtomicBoolean cTyped = new AtomicBoolean(false);
|
||||
|
||||
public static void main(String[] args) throws InterruptedException,
|
||||
InvocationTargetException, AWTException {
|
||||
KeyEventToLightweight test = new KeyEventToLightweight();
|
||||
try {
|
||||
EventQueue.invokeAndWait(test::initUI);
|
||||
test.performTest();
|
||||
} finally {
|
||||
EventQueue.invokeAndWait(test::dispose);
|
||||
}
|
||||
}
|
||||
|
||||
public void initUI() {
|
||||
this.setLayout(new FlowLayout());
|
||||
add(lwbutton);
|
||||
add(hwbutton);
|
||||
setSize(200, 200);
|
||||
setLocationRelativeTo(null);
|
||||
lwbutton.addKeyListener(new KeyAdapter() {
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (e.getKeyCode() == KeyEvent.VK_A) {
|
||||
aTyped.set(true);
|
||||
} else if (e.getKeyCode() == KeyEvent.VK_B) {
|
||||
bTyped.set(true);
|
||||
} else if (e.getKeyCode() == KeyEvent.VK_C) {
|
||||
cTyped.set(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
public void middleOf(Component c, Point p) {
|
||||
Point loc = c.getLocationOnScreen();
|
||||
Dimension size = c.getSize();
|
||||
p.setLocation(loc.x + (size.width / 2), loc.y + (size.height / 2));
|
||||
}
|
||||
|
||||
public void performTest() throws AWTException, InterruptedException,
|
||||
InvocationTargetException {
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoDelay(50);
|
||||
robot.delay(500);
|
||||
robot.waitForIdle();
|
||||
Point target = new Point();
|
||||
EventQueue.invokeAndWait(() -> middleOf(lwbutton, target));
|
||||
robot.mouseMove(target.x, target.y);
|
||||
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
|
||||
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
|
||||
robot.waitForIdle();
|
||||
robot.delay(500);
|
||||
robot.keyPress(KeyEvent.VK_A);
|
||||
robot.keyRelease(KeyEvent.VK_A);
|
||||
robot.waitForIdle();
|
||||
robot.mouseMove(target.x - 200, target.y);
|
||||
robot.keyPress(KeyEvent.VK_B);
|
||||
robot.keyRelease(KeyEvent.VK_B);
|
||||
robot.waitForIdle();
|
||||
robot.delay(500);
|
||||
EventQueue.invokeAndWait(() -> middleOf(hwbutton, target));
|
||||
robot.mouseMove(target.x, target.y);
|
||||
robot.keyPress(KeyEvent.VK_C);
|
||||
robot.keyRelease(KeyEvent.VK_C);
|
||||
if (!aTyped.get() || !bTyped.get() || !cTyped.get()) {
|
||||
throw new RuntimeException("Key event was not delivered, case 1: "
|
||||
+ aTyped.get() + ", case 2: " + bTyped.get() + ", case 3: "
|
||||
+ cTyped.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
229
test/jdk/java/awt/event/KeyEvent/KeyMaskTest/KeyMaskTest.java
Normal file
229
test/jdk/java/awt/event/KeyEvent/KeyMaskTest/KeyMaskTest.java
Normal file
|
|
@ -0,0 +1,229 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2022, 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.
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 8043126
|
||||
* @summary Check whether KeyEvent.getModifiers() returns correct modifiers
|
||||
* when Ctrl, Alt or Shift keys are pressed.
|
||||
*
|
||||
* @library /lib/client/ ../../helpers/lwcomponents/
|
||||
* @library /test/lib
|
||||
* @build LWComponent
|
||||
* @build LWButton
|
||||
* @build LWList
|
||||
* @build ExtendedRobot
|
||||
* @run main/timeout=300 KeyMaskTest
|
||||
*/
|
||||
|
||||
|
||||
import java.awt.Button;
|
||||
import java.awt.Color;
|
||||
import java.awt.Component;
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.Frame;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.List;
|
||||
import java.awt.Point;
|
||||
import java.awt.TextArea;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.InputEvent;
|
||||
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static jdk.test.lib.Asserts.assertEQ;
|
||||
import static jdk.test.lib.Asserts.assertTrue;
|
||||
|
||||
import test.java.awt.event.helpers.lwcomponents.LWButton;
|
||||
import test.java.awt.event.helpers.lwcomponents.LWList;
|
||||
|
||||
|
||||
|
||||
public class KeyMaskTest extends KeyAdapter {
|
||||
|
||||
Frame frame;
|
||||
|
||||
Button button;
|
||||
LWButton buttonLW;
|
||||
TextField textField;
|
||||
TextArea textArea;
|
||||
List list;
|
||||
LWList listLW;
|
||||
|
||||
ExtendedRobot robot;
|
||||
|
||||
private final static int robotDelay = 500;
|
||||
private final static int waitDelay = 3500;
|
||||
|
||||
final Object lock;
|
||||
|
||||
private boolean keyPressReceived = false;
|
||||
private int keyCode = -1;
|
||||
|
||||
KeyMaskTest() throws Exception {
|
||||
lock = new Object();
|
||||
robot = new ExtendedRobot();
|
||||
EventQueue.invokeAndWait( this::createGUI );
|
||||
}
|
||||
|
||||
public void createGUI() {
|
||||
|
||||
frame = new Frame();
|
||||
frame.setTitle("KeyMaskTest");
|
||||
frame.setLayout(new GridLayout(1, 6));
|
||||
frame.setLocationRelativeTo(null);
|
||||
|
||||
button = new Button();
|
||||
button.addKeyListener(this);
|
||||
frame.add(button);
|
||||
|
||||
buttonLW = new LWButton();
|
||||
buttonLW.addKeyListener(this);
|
||||
frame.add(buttonLW);
|
||||
|
||||
textField = new TextField(5);
|
||||
textField.addKeyListener(this);
|
||||
frame.add(textField);
|
||||
|
||||
textArea = new TextArea(5, 5);
|
||||
textArea.addKeyListener(this);
|
||||
frame.add(textArea);
|
||||
|
||||
list = new List();
|
||||
for (int i = 1; i <= 5; ++i) { list.add("item " + i); }
|
||||
list.addKeyListener(this);
|
||||
frame.add(list);
|
||||
|
||||
listLW = new LWList();
|
||||
for (int i = 1; i <= 5; ++i) { listLW.add("item " + i); }
|
||||
listLW.addKeyListener(this);
|
||||
frame.add(listLW);
|
||||
|
||||
|
||||
frame.setBackground(Color.gray);
|
||||
frame.setSize(500, 100);
|
||||
frame.setVisible(true);
|
||||
frame.toFront();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
keyPressReceived = true;
|
||||
|
||||
int code = e.getKeyCode();
|
||||
|
||||
assertEQ(code, keyCode, "wrong key code");
|
||||
|
||||
int mask = 0;
|
||||
|
||||
if (code == KeyEvent.VK_SHIFT) {
|
||||
mask = InputEvent.SHIFT_MASK;
|
||||
} else if (code == KeyEvent.VK_CONTROL) {
|
||||
mask = InputEvent.CTRL_MASK;
|
||||
} else if (code == KeyEvent.VK_ALT) {
|
||||
mask = InputEvent.ALT_MASK;
|
||||
} else if (code == KeyEvent.VK_META) {
|
||||
mask = InputEvent.META_MASK;
|
||||
}
|
||||
|
||||
int mod = e.getModifiers() & mask;
|
||||
assertEQ(mod, mask, "invalid key mask");
|
||||
|
||||
synchronized (lock) { lock.notifyAll(); }
|
||||
}
|
||||
|
||||
|
||||
void doTest() throws Exception {
|
||||
|
||||
ArrayList<Component> components = new ArrayList();
|
||||
components.add(button);
|
||||
components.add(buttonLW);
|
||||
components.add(textField);
|
||||
components.add(textArea);
|
||||
components.add(list);
|
||||
components.add(listLW);
|
||||
|
||||
int keys[];
|
||||
String OS = System.getProperty("os.name").toLowerCase();
|
||||
System.out.println(OS);
|
||||
if (OS.contains("os x")) {
|
||||
keys = new int[] {KeyEvent.VK_SHIFT, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_META};
|
||||
} else {
|
||||
keys = new int[] {KeyEvent.VK_SHIFT, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT};
|
||||
}
|
||||
|
||||
for (Component c: components) {
|
||||
|
||||
System.out.print(c.getClass().getName() + ": ");
|
||||
|
||||
Point origin = c.getLocationOnScreen();
|
||||
int xc = origin.x + c.getWidth() / 2;
|
||||
int yc = origin.y + c.getHeight() / 2;
|
||||
Point center = new Point(xc, yc);
|
||||
|
||||
robot.delay(robotDelay);
|
||||
robot.glide(origin, center);
|
||||
robot.click();
|
||||
robot.delay(robotDelay);
|
||||
|
||||
for (int k = 0; k < keys.length; ++k) {
|
||||
|
||||
keyPressReceived = false;
|
||||
|
||||
keyCode = keys[k];
|
||||
|
||||
robot.type(keyCode);
|
||||
|
||||
robot.delay(robotDelay);
|
||||
|
||||
if (!keyPressReceived) {
|
||||
synchronized (lock) {
|
||||
try {
|
||||
lock.wait(waitDelay);
|
||||
} catch (InterruptedException e) {}
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(keyPressReceived, "key press event was not received");
|
||||
}
|
||||
|
||||
System.out.println("passed");
|
||||
}
|
||||
|
||||
robot.waitForIdle();
|
||||
frame.dispose();
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
KeyMaskTest test = new KeyMaskTest();
|
||||
test.doTest();
|
||||
}
|
||||
}
|
||||
134
test/jdk/java/awt/event/KeyEvent/KeyModifiers.java
Normal file
134
test/jdk/java/awt/event/KeyEvent/KeyModifiers.java
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4193779 4174399
|
||||
* @summary Ensures that KeyEvents have the right modifiers set
|
||||
* @library /java/awt/regtesthelpers /test/lib
|
||||
* @build PassFailJFrame jdk.test.lib.Platform
|
||||
* @run main/manual KeyModifiers
|
||||
*/
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Frame;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.InputEvent;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import jdk.test.lib.Platform;
|
||||
|
||||
|
||||
public class KeyModifiers extends Frame implements KeyListener {
|
||||
public KeyModifiers() {
|
||||
super("Check KeyChar values");
|
||||
setLayout(new BorderLayout());
|
||||
TextField tf = new TextField(30);
|
||||
tf.addKeyListener(this);
|
||||
add(tf, BorderLayout.CENTER);
|
||||
pack();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException,
|
||||
InvocationTargetException {
|
||||
|
||||
String keys;
|
||||
if (Platform.isWindows()) {
|
||||
keys = "\"Shift-n\", \"Alt-n\"\n";
|
||||
} else if (Platform.isOSX()) {
|
||||
keys = "\"Shift-n\", \"Alt-n\", \"Command-n\"\n";
|
||||
} else {
|
||||
keys = "\"Shift-n\", \"Alt-n\", \"Meta-n\"\n";
|
||||
}
|
||||
|
||||
String INSTRUCTIONS1 = """
|
||||
Click on the text field in the window named "Check KeyChar values"
|
||||
and type the following key combinations:
|
||||
""";
|
||||
String INSTRUCTIONS2 = """
|
||||
After each combination check that the KeyPressed and KeyTyped modifiers
|
||||
are correctly displayed. If modifiers are correct press "Pass",
|
||||
otherwise press "Fail".
|
||||
""";
|
||||
PassFailJFrame.builder()
|
||||
.title("KeyModifiers Test Instructions")
|
||||
.instructions(INSTRUCTIONS1 + keys + INSTRUCTIONS2)
|
||||
.columns(45)
|
||||
.logArea(10)
|
||||
.testUI(KeyModifiers::new)
|
||||
.build()
|
||||
.awaitAndCheck();
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent evt) {
|
||||
int kc = evt.getKeyCode();
|
||||
|
||||
if (kc == KeyEvent.VK_CONTROL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((kc == KeyEvent.VK_SHIFT) || (kc == KeyEvent.VK_META) ||
|
||||
(kc == KeyEvent.VK_ALT) || (kc == KeyEvent.VK_ALT_GRAPH)) {
|
||||
PassFailJFrame.log("Key pressed= " + KeyEvent.getKeyText(kc) +
|
||||
" modifiers = " + InputEvent.getModifiersExText(evt.getModifiersEx()));
|
||||
} else {
|
||||
PassFailJFrame.log("Key pressed = " + evt.getKeyChar() +
|
||||
" modifiers = " + InputEvent.getModifiersExText(evt.getModifiersEx()));
|
||||
}
|
||||
}
|
||||
|
||||
public void keyTyped(KeyEvent evt) {
|
||||
int kc = evt.getKeyCode();
|
||||
|
||||
if (kc == KeyEvent.VK_CONTROL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((kc == KeyEvent.VK_SHIFT) || (kc == KeyEvent.VK_META) ||
|
||||
(kc == KeyEvent.VK_ALT) || (kc == KeyEvent.VK_ALT_GRAPH)) {
|
||||
PassFailJFrame.log("Key typed = " + KeyEvent.getKeyText(kc) +
|
||||
" modifiers = " + InputEvent.getModifiersExText(evt.getModifiersEx()));
|
||||
} else {
|
||||
PassFailJFrame.log("Key typed = " + evt.getKeyChar() +
|
||||
" modifiers = " + InputEvent.getModifiersExText(evt.getModifiersEx()));
|
||||
}
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent evt) {
|
||||
int kc = evt.getKeyCode();
|
||||
|
||||
if (kc == KeyEvent.VK_CONTROL)
|
||||
return;
|
||||
|
||||
if ((kc == KeyEvent.VK_SHIFT) || (kc == KeyEvent.VK_META) ||
|
||||
(kc == KeyEvent.VK_ALT) || (kc == KeyEvent.VK_ALT_GRAPH)) {
|
||||
PassFailJFrame.log("Key = released " + KeyEvent.getKeyText(kc) +
|
||||
" modifiers = " + InputEvent.getModifiersExText(evt.getModifiersEx()));
|
||||
} else {
|
||||
PassFailJFrame.log("Key released = " + evt.getKeyChar() +
|
||||
" modifiers = " + InputEvent.getModifiersExText(evt.getModifiersEx()));
|
||||
}
|
||||
}
|
||||
}
|
||||
108
test/jdk/java/awt/event/KeyEvent/KeyPressedModifiers.java
Normal file
108
test/jdk/java/awt/event/KeyEvent/KeyPressedModifiers.java
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4174399
|
||||
* @summary Check that modifier values are set on a KeyPressed event
|
||||
* when a modifier key is pressed.
|
||||
* @key headful
|
||||
* @run main KeyPressedModifiers
|
||||
*/
|
||||
|
||||
import java.awt.AWTException;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.Frame;
|
||||
import java.awt.Robot;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class KeyPressedModifiers extends Frame implements KeyListener {
|
||||
static AtomicBoolean shiftDown = new AtomicBoolean(false);
|
||||
static AtomicBoolean controlDown = new AtomicBoolean(false);
|
||||
static AtomicBoolean altDown = new AtomicBoolean(false);
|
||||
|
||||
public static void main(String[] args) throws InterruptedException,
|
||||
InvocationTargetException, AWTException {
|
||||
KeyPressedModifiers test = new KeyPressedModifiers();
|
||||
try {
|
||||
EventQueue.invokeAndWait(test::initUI);
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoDelay(100);
|
||||
robot.delay(500);
|
||||
robot.waitForIdle();
|
||||
robot.keyPress(KeyEvent.VK_SHIFT);
|
||||
robot.keyRelease(KeyEvent.VK_SHIFT);
|
||||
robot.keyPress(KeyEvent.VK_CONTROL);
|
||||
robot.keyRelease(KeyEvent.VK_CONTROL);
|
||||
robot.keyPress(KeyEvent.VK_ALT);
|
||||
robot.keyRelease(KeyEvent.VK_ALT);
|
||||
robot.delay(500);
|
||||
robot.waitForIdle();
|
||||
if (!shiftDown.get() || !controlDown.get() || !altDown.get()) {
|
||||
String error = "Following key modifiers were not registered:" +
|
||||
(shiftDown.get() ? "" : " SHIFT") +
|
||||
(controlDown.get() ? "" : " CONTROL") +
|
||||
(altDown.get() ? "" : " ALT");
|
||||
throw new RuntimeException(error);
|
||||
}
|
||||
} finally {
|
||||
EventQueue.invokeAndWait(test::dispose);
|
||||
}
|
||||
}
|
||||
|
||||
public void initUI() {
|
||||
setLayout(new BorderLayout());
|
||||
TextField tf = new TextField(30);
|
||||
tf.addKeyListener(this);
|
||||
add(tf, BorderLayout.CENTER);
|
||||
setSize(350, 100);
|
||||
setVisible(true);
|
||||
tf.requestFocus();
|
||||
}
|
||||
|
||||
public void keyTyped(KeyEvent ignore) {
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent ignore) {
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
System.out.println(e);
|
||||
switch (e.getKeyCode()) {
|
||||
case KeyEvent.VK_SHIFT:
|
||||
shiftDown.set(e.isShiftDown());
|
||||
break;
|
||||
case KeyEvent.VK_CONTROL:
|
||||
controlDown.set(e.isControlDown());
|
||||
break;
|
||||
case KeyEvent.VK_ALT:
|
||||
altDown.set(e.isAltDown());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
104
test/jdk/java/awt/event/KeyEvent/KeyTest.java
Normal file
104
test/jdk/java/awt/event/KeyEvent/KeyTest.java
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4151419 4090870 4169733
|
||||
* @summary Ensures that KeyEvent has right results for the following
|
||||
* keys -=\[];,./
|
||||
* @library /java/awt/regtesthelpers
|
||||
* @build PassFailJFrame
|
||||
* @run main/manual KeyTest
|
||||
*/
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Frame;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class KeyTest extends Frame implements KeyListener {
|
||||
|
||||
static String INSTRUCTIONS = """
|
||||
Click on the text field in window named "Check KeyChar values"
|
||||
Type the following keys/characters in the TextField:
|
||||
- = \\ [ ] ; , . /
|
||||
Verify that the keyChar and keyCode is correct for each key pressed.
|
||||
Remember that the keyCode for the KEY_TYPED event should be zero.
|
||||
Also verify that the character you typed appears in the TextField.
|
||||
|
||||
Key Name keyChar Keycode
|
||||
-------------------------------------
|
||||
- Minus - 45 45
|
||||
= Equals = 61 61
|
||||
\\ Slash \\ 92 92
|
||||
[ Left Brace [ 91 91
|
||||
] Right Brace ] 93 93
|
||||
; Semicolon ; 59 59
|
||||
, Comma , 44 44
|
||||
. Period . 46 46
|
||||
/ Front Slash / 47 47
|
||||
""";
|
||||
public KeyTest() {
|
||||
super("Check KeyChar values");
|
||||
setLayout(new BorderLayout());
|
||||
TextField tf = new TextField(30);
|
||||
tf.addKeyListener(this);
|
||||
add(tf, BorderLayout.CENTER);
|
||||
pack();
|
||||
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
}
|
||||
|
||||
public void keyTyped(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
}
|
||||
|
||||
protected void printKey(KeyEvent evt) {
|
||||
if (evt.isActionKey()) {
|
||||
PassFailJFrame.log("params= " + evt.paramString() + " KeyChar: " +
|
||||
(int) evt.getKeyChar() + " Action Key");
|
||||
} else {
|
||||
PassFailJFrame.log("params= " + evt.paramString() + " KeyChar: " +
|
||||
(int) evt.getKeyChar());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException, InvocationTargetException {
|
||||
PassFailJFrame.builder()
|
||||
.title("KeyTest Instructions")
|
||||
.instructions(INSTRUCTIONS)
|
||||
.logArea(20)
|
||||
.testUI(KeyTest::new)
|
||||
.build()
|
||||
.awaitAndCheck();
|
||||
}
|
||||
}
|
||||
138
test/jdk/java/awt/event/KeyEvent/KeyTyped/CancelKeyTyped.java
Normal file
138
test/jdk/java/awt/event/KeyEvent/KeyTyped/CancelKeyTyped.java
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
/*
|
||||
* Copyright (c) 2002, 2023, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4754155
|
||||
* @summary Tests that KeyTyped events are fired for the Cancel key
|
||||
* and that no extraneous characters are entered as a result
|
||||
* @key headful
|
||||
* @run main CancelKeyTyped
|
||||
*/
|
||||
|
||||
import java.awt.AWTException;
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.Frame;
|
||||
import java.awt.Robot;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
|
||||
public class CancelKeyTyped {
|
||||
static volatile boolean cancelKeyTypedReceived = false;
|
||||
static Frame frame;
|
||||
static TextField tf;
|
||||
static final String ORIGINAL = "0123456789";
|
||||
|
||||
public static void main(String[] args) throws AWTException,
|
||||
InterruptedException, InvocationTargetException {
|
||||
try {
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoWaitForIdle(true);
|
||||
robot.setAutoDelay(100);
|
||||
|
||||
EventQueue.invokeAndWait(CancelKeyTyped::init);
|
||||
robot.waitForIdle();
|
||||
robot.delay(1000);
|
||||
|
||||
// Press and release Cancel
|
||||
robot.keyPress(KeyEvent.VK_CANCEL);
|
||||
robot.keyRelease(KeyEvent.VK_CANCEL);
|
||||
|
||||
if (cancelKeyTypedReceived) {
|
||||
if (tf.getText().equals(ORIGINAL)) {
|
||||
System.out.println("Test PASSED");
|
||||
} else {
|
||||
System.out.println("Test FAILED: wrong string");
|
||||
throw new RuntimeException("The test failed: wrong string: "
|
||||
+ tf.getText());
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
EventQueue.invokeAndWait(() -> {
|
||||
if (frame != null) {
|
||||
frame.dispose();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
frame = new Frame("CancelKeyTyped");
|
||||
tf = new TextField(ORIGINAL, 20);
|
||||
frame.add(tf);
|
||||
frame.setSize(300, 100);
|
||||
frame.setVisible(true);
|
||||
frame.validate();
|
||||
|
||||
tf.requestFocusInWindow();
|
||||
|
||||
tf.addKeyListener(new KeyListener() {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
|
||||
int keychar = evt.getKeyChar();
|
||||
if (keychar == 24) { // Cancel character is 24 or \u0018
|
||||
cancelKeyTypedReceived = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
}
|
||||
|
||||
protected void printKey(KeyEvent evt) {
|
||||
switch (evt.getID()) {
|
||||
case KeyEvent.KEY_TYPED:
|
||||
case KeyEvent.KEY_PRESSED:
|
||||
case KeyEvent.KEY_RELEASED:
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("params= " + evt.paramString() + " \n" +
|
||||
"KeyChar: " + evt.getKeyChar() + " = " +
|
||||
(int) evt.getKeyChar() +
|
||||
" KeyCode: " + evt.getKeyCode() +
|
||||
" Modifiers: " + evt.getModifiersEx());
|
||||
|
||||
if (evt.isActionKey()) {
|
||||
System.out.println("Action Key");
|
||||
}
|
||||
|
||||
System.out.println("keyText= " +
|
||||
evt.getKeyText(evt.getKeyCode()) + "\n");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
269
test/jdk/java/awt/event/KeyEvent/KeyTyped/CtrlASCII.java
Normal file
269
test/jdk/java/awt/event/KeyEvent/KeyTyped/CtrlASCII.java
Normal file
|
|
@ -0,0 +1,269 @@
|
|||
/*
|
||||
* Copyright (c) 2007, 2022, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
@test
|
||||
@key headful
|
||||
@bug 6497426
|
||||
@summary Tests that pressing of Ctrl+ascii mostly fires KEY_TYPED with a Unicode control symbols
|
||||
@run main/timeout=600 CtrlASCII
|
||||
*/
|
||||
|
||||
import java.awt.AWTException;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Frame;
|
||||
import java.awt.Point;
|
||||
import java.awt.Robot;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.InputEvent;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.util.HashMap;
|
||||
|
||||
//
|
||||
// In this test, a key listener for KEY_TYPED checks if a character typed has
|
||||
// a correspondent keycode in an initially filled hashtable.
|
||||
// If it does not, test fails. If character was produced by
|
||||
// pressing a wrong key still listed in the hashtable, test cannot detect it.
|
||||
// Under MS Windows, unlike X Window, some Ctrl+Ascii keystrokes don't
|
||||
// produce a unicode character, so there will be no KEY_TYPED and no problem.
|
||||
// Test doesn't try to verify Ctrl+deadkey behavior.
|
||||
//
|
||||
public class CtrlASCII extends Frame implements KeyListener {
|
||||
// Declare things used in the test, like buttons and labels here
|
||||
static final HashMap<Character, Integer> KEYCHAR_MAP = new HashMap<>();
|
||||
static volatile boolean testFailed = false;
|
||||
|
||||
TextField tf;
|
||||
Robot robot;
|
||||
|
||||
static void fillHash() {
|
||||
KEYCHAR_MAP.put((char) 0x20, KeyEvent.VK_SPACE); /*32,x20*/ /*' ' */
|
||||
KEYCHAR_MAP.put((char) 0x21, KeyEvent.VK_EXCLAMATION_MARK); /*33,x21*/ /*'!' fr*/
|
||||
KEYCHAR_MAP.put((char) 0x22, KeyEvent.VK_QUOTEDBL); /*34,x22*/ /*'"' fr*/
|
||||
KEYCHAR_MAP.put((char) 0x23, KeyEvent.VK_NUMBER_SIGN); /*35,x23*/ /*'#' de*/
|
||||
KEYCHAR_MAP.put((char) 0x24, KeyEvent.VK_DOLLAR); /*36,x24*/ /*'$', de_CH*/
|
||||
//keycharHash.put('%', (char)0x25 ); /*37,x25*/ /*no VK, cannot test*/
|
||||
KEYCHAR_MAP.put((char) 0x26, KeyEvent.VK_AMPERSAND); /*38,x26*/ /*'&', fr*/
|
||||
KEYCHAR_MAP.put((char) 0x27, KeyEvent.VK_QUOTE); /*39,x27*/ /*''', fr*/
|
||||
KEYCHAR_MAP.put((char) 0x28, KeyEvent.VK_LEFT_PARENTHESIS); /*40,x28*/ /*'(', fr*/
|
||||
KEYCHAR_MAP.put((char) 0x29, KeyEvent.VK_RIGHT_PARENTHESIS); /*41,x29*/ /*')', fr*/
|
||||
KEYCHAR_MAP.put((char) 0x2a, KeyEvent.VK_ASTERISK); /*42,x2a*/ /*'*', fr*/
|
||||
KEYCHAR_MAP.put((char) 0x2b, KeyEvent.VK_PLUS); /*43,x2b*/ /*'+', de*/
|
||||
KEYCHAR_MAP.put((char) 0x2c, KeyEvent.VK_COMMA); /*44,x2c*/ /*','*/
|
||||
KEYCHAR_MAP.put((char) 0x2d, KeyEvent.VK_MINUS); /*45,x2d*/ /*'-'*/
|
||||
KEYCHAR_MAP.put((char) 0x2e, KeyEvent.VK_PERIOD); /*46,x2e*/ /*'.'*/
|
||||
KEYCHAR_MAP.put((char) 0x2f, KeyEvent.VK_SLASH); /*47,x2f*/ /*'/'*/
|
||||
KEYCHAR_MAP.put((char) 0x30, KeyEvent.VK_0); /*48,x30*/
|
||||
KEYCHAR_MAP.put((char) 0x31, KeyEvent.VK_1); /*49,x31*/
|
||||
KEYCHAR_MAP.put((char) 0x32, KeyEvent.VK_2); /*50,x32*/
|
||||
KEYCHAR_MAP.put((char) 0x33, KeyEvent.VK_3); /*51,x33*/
|
||||
KEYCHAR_MAP.put((char) 0x34, KeyEvent.VK_4); /*52,x34*/
|
||||
KEYCHAR_MAP.put((char) 0x35, KeyEvent.VK_5); /*53,x35*/
|
||||
KEYCHAR_MAP.put((char) 0x36, KeyEvent.VK_6); /*54,x36*/
|
||||
KEYCHAR_MAP.put((char) 0x37, KeyEvent.VK_7); /*55,x37*/
|
||||
KEYCHAR_MAP.put((char) 0x38, KeyEvent.VK_8); /*56,x38*/
|
||||
KEYCHAR_MAP.put((char) 0x39, KeyEvent.VK_9); /*57,x39*/
|
||||
KEYCHAR_MAP.put((char) 0x3a, KeyEvent.VK_COLON); /*58,x3a*/ /*':', fr*/
|
||||
KEYCHAR_MAP.put((char) 0x3b, KeyEvent.VK_SEMICOLON); /*59,x3b*/ /*';'*/
|
||||
KEYCHAR_MAP.put((char) 0x3c, KeyEvent.VK_LESS); /*60,x3c*/ /*'<' us 102*/
|
||||
KEYCHAR_MAP.put((char) 0x3d, KeyEvent.VK_EQUALS); /*61,x3d*/
|
||||
KEYCHAR_MAP.put((char) 0x3e, KeyEvent.VK_GREATER); /*62,x3e*/ /*'>' ?????? where???*/
|
||||
// Javadoc says: "there is no keycode for the question mark because
|
||||
// there is no keyboard for which it appears on the primary layer."
|
||||
// Well, it's Lithuanian standard.
|
||||
//keycharHash.put('?', (char)0x3f); /*63,x3f*/ /*no VK, cannot test*/
|
||||
KEYCHAR_MAP.put((char) 0x40, KeyEvent.VK_AT); /*64,x40*/ /*'@' ?????? where???*/
|
||||
KEYCHAR_MAP.put((char) 0x1, KeyEvent.VK_A); /*65,x41*/
|
||||
KEYCHAR_MAP.put((char) 0x2, KeyEvent.VK_B); /*66,x42*/
|
||||
KEYCHAR_MAP.put((char) 0x3, KeyEvent.VK_C); /*67,x43*/
|
||||
KEYCHAR_MAP.put((char) 0x4, KeyEvent.VK_D); /*68,x44*/
|
||||
KEYCHAR_MAP.put((char) 0x5, KeyEvent.VK_E); /*69,x45*/
|
||||
KEYCHAR_MAP.put((char) 0x6, KeyEvent.VK_F); /*70,x46*/
|
||||
KEYCHAR_MAP.put((char) 0x7, KeyEvent.VK_G); /*71,x47*/
|
||||
KEYCHAR_MAP.put((char) 0x8, KeyEvent.VK_H); /*72,x48*/
|
||||
KEYCHAR_MAP.put((char) 0x9, KeyEvent.VK_I); /*73,x49*/
|
||||
KEYCHAR_MAP.put((char) 0xa, KeyEvent.VK_J); /*74,x4a*/
|
||||
KEYCHAR_MAP.put((char) 0xb, KeyEvent.VK_K); /*75,x4b*/
|
||||
KEYCHAR_MAP.put((char) 0xc, KeyEvent.VK_L); /*76,x4c*/
|
||||
KEYCHAR_MAP.put((char) 0xd, KeyEvent.VK_M); /*77,x4d*/
|
||||
KEYCHAR_MAP.put((char) 0xe, KeyEvent.VK_N); /*78,x4e*/
|
||||
KEYCHAR_MAP.put((char) 0xf, KeyEvent.VK_O); /*79,x4f*/
|
||||
KEYCHAR_MAP.put((char) 0x10, KeyEvent.VK_P); /*80,x50*/
|
||||
KEYCHAR_MAP.put((char) 0x11, KeyEvent.VK_Q); /*81,x51*/
|
||||
KEYCHAR_MAP.put((char) 0x12, KeyEvent.VK_R); /*82,x52*/
|
||||
KEYCHAR_MAP.put((char) 0x13, KeyEvent.VK_S); /*83,x53*/
|
||||
KEYCHAR_MAP.put((char) 0x14, KeyEvent.VK_T); /*84,x54*/
|
||||
KEYCHAR_MAP.put((char) 0x15, KeyEvent.VK_U); /*85,x55*/
|
||||
KEYCHAR_MAP.put((char) 0x16, KeyEvent.VK_V); /*86,x56*/
|
||||
KEYCHAR_MAP.put((char) 0x17, KeyEvent.VK_W); /*87,x57*/
|
||||
KEYCHAR_MAP.put((char) 0x18, KeyEvent.VK_X); /*88,x58*/
|
||||
KEYCHAR_MAP.put((char) 0x19, KeyEvent.VK_Y); /*89,x59*/
|
||||
KEYCHAR_MAP.put((char) 0x1a, KeyEvent.VK_Z); /*90,x5a*/
|
||||
|
||||
KEYCHAR_MAP.put((char) 0x1b, KeyEvent.VK_OPEN_BRACKET); /*91,x5b*/ /*'['*/
|
||||
KEYCHAR_MAP.put((char) 0x1c, KeyEvent.VK_BACK_SLASH); /*92,x5c*/ /*'\'*/
|
||||
KEYCHAR_MAP.put((char) 0x1d, KeyEvent.VK_CLOSE_BRACKET); /*93,x5d*/ /*']'*/
|
||||
KEYCHAR_MAP.put((char) 0x5e, KeyEvent.VK_CIRCUMFLEX); /*94,x5e*/ /*'^' ?? nodead fr, de??*/
|
||||
KEYCHAR_MAP.put((char) 0x1f, KeyEvent.VK_UNDERSCORE); /*95,x5f*/ /*'_' fr*/
|
||||
KEYCHAR_MAP.put((char) 0x60, KeyEvent.VK_BACK_QUOTE); /*96,x60*/
|
||||
|
||||
/********* Same as uppercase*/
|
||||
//keycharHash.put((char)0x1, KeyEvent.VK_a); /*97,x61*/
|
||||
//keycharHash.put((char)0x2, KeyEvent.VK_b); /*98,x62*/
|
||||
//keycharHash.put((char)0x3, KeyEvent.VK_c); /*99,x63*/
|
||||
//keycharHash.put((char)0x4, KeyEvent.VK_d); /*100,x64*/
|
||||
//keycharHash.put((char)0x5, KeyEvent.VK_e); /*101,x65*/
|
||||
//keycharHash.put((char)0x6, KeyEvent.VK_f); /*102,x66*/
|
||||
//keycharHash.put((char)0x7, KeyEvent.VK_g); /*103,x67*/
|
||||
//keycharHash.put((char)0x8, KeyEvent.VK_h); /*104,x68*/
|
||||
//keycharHash.put((char)0x9, KeyEvent.VK_i); /*105,x69*/
|
||||
//keycharHash.put((char)0xa, KeyEvent.VK_j); /*106,x6a*/
|
||||
//keycharHash.put((char)0xb, KeyEvent.VK_k); /*107,x6b*/
|
||||
//keycharHash.put((char)0xc, KeyEvent.VK_l); /*108,x6c*/
|
||||
//keycharHash.put((char)0xd, KeyEvent.VK_m); /*109,x6d*/
|
||||
//keycharHash.put((char)0xe, KeyEvent.VK_n); /*110,x6e*/
|
||||
//keycharHash.put((char)0xf, KeyEvent.VK_o); /*111,x6f*/
|
||||
//keycharHash.put((char)0x10, KeyEvent.VK_p); /*112,x70*/
|
||||
//keycharHash.put((char)0x11, KeyEvent.VK_q); /*113,x71*/
|
||||
//keycharHash.put((char)0x12, KeyEvent.VK_r); /*114,x72*/
|
||||
//keycharHash.put((char)0x13, KeyEvent.VK_s); /*115,x73*/
|
||||
//keycharHash.put((char)0x14, KeyEvent.VK_t); /*116,x74*/
|
||||
//keycharHash.put((char)0x15, KeyEvent.VK_u); /*117,x75*/
|
||||
//keycharHash.put((char)0x16, KeyEvent.VK_v); /*118,x76*/
|
||||
//keycharHash.put((char)0x17, KeyEvent.VK_w); /*119,x77*/
|
||||
//keycharHash.put((char)0x18, KeyEvent.VK_x); /*120,x78*/
|
||||
//keycharHash.put((char)0x19, KeyEvent.VK_y); /*121,x79*/
|
||||
//keycharHash.put((char)0x1a, KeyEvent.VK_z); /*122,x7a*/
|
||||
|
||||
KEYCHAR_MAP.put((char) 0x7b, KeyEvent.VK_BRACELEFT); /*123,x7b*/ /*'{' la (Latin American)*/
|
||||
//keycharHash.put(char) 0x1c, KeyEvent.VK_|); /*124,x7c*/ /* no VK, cannot test*/
|
||||
KEYCHAR_MAP.put((char) 0x7d, KeyEvent.VK_BRACERIGHT); /*125,x7d*/ /*'}' la */
|
||||
//keycharHash.put((char) 0x1e, KeyEvent.VK_~); /*126,x7e*/ /* no VK, cannot test*/
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws AWTException {
|
||||
CtrlASCII test = new CtrlASCII();
|
||||
test.init();
|
||||
test.start();
|
||||
}
|
||||
|
||||
public void init() throws AWTException {
|
||||
fillHash();
|
||||
|
||||
robot = new Robot();
|
||||
robot.setAutoWaitForIdle(true);
|
||||
robot.setAutoDelay(100);
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
setSize(400, 300);
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
|
||||
String original = "0123456789";
|
||||
tf = new TextField(original, 20);
|
||||
this.add(tf);
|
||||
tf.addKeyListener(this);
|
||||
validate();
|
||||
|
||||
robot.waitForIdle();
|
||||
robot.delay(1000);
|
||||
|
||||
this.requestFocus();
|
||||
tf.requestFocusInWindow();
|
||||
|
||||
robot.waitForIdle();
|
||||
}//End init()
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
Point pt = getLocationOnScreen();
|
||||
robot.mouseMove(pt.x + 100, pt.y + 100);
|
||||
robot.delay(1000);
|
||||
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
|
||||
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
|
||||
|
||||
KEYCHAR_MAP.forEach((k, v) -> punchCtrlKey(v));
|
||||
|
||||
robot.delay(500);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("The test was not completed.\n\n" + e);
|
||||
} finally {
|
||||
dispose();
|
||||
}
|
||||
if (testFailed) {
|
||||
throw new RuntimeException("The test failed.\n\n");
|
||||
}
|
||||
System.out.println("Success\n");
|
||||
|
||||
}// start()
|
||||
|
||||
public void punchCtrlKey(int keyCode) {
|
||||
try {
|
||||
robot.keyPress(KeyEvent.VK_CONTROL);
|
||||
robot.keyPress(keyCode);
|
||||
} catch (IllegalArgumentException iae) {
|
||||
System.err.println("skip probably invalid keyCode " + keyCode);
|
||||
} finally {
|
||||
try {
|
||||
robot.keyRelease(keyCode);
|
||||
} catch (IllegalArgumentException iae) {
|
||||
System.err.println("skip probably invalid keyCode " + keyCode);
|
||||
}
|
||||
robot.keyRelease(KeyEvent.VK_CONTROL);
|
||||
}
|
||||
robot.delay(200);
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent evt) {
|
||||
//printKey(evt);
|
||||
}
|
||||
|
||||
public void keyTyped(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
char keych = evt.getKeyChar();
|
||||
if (!KEYCHAR_MAP.containsKey(keych)) {
|
||||
System.out.println("Unexpected keychar: " + keych);
|
||||
testFailed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent evt) {
|
||||
//printKey(evt);
|
||||
}
|
||||
|
||||
protected void printKey(KeyEvent evt) {
|
||||
switch (evt.getID()) {
|
||||
case KeyEvent.KEY_TYPED:
|
||||
case KeyEvent.KEY_PRESSED:
|
||||
case KeyEvent.KEY_RELEASED:
|
||||
break;
|
||||
default:
|
||||
System.out.println("Other Event ");
|
||||
return;
|
||||
}
|
||||
System.out.println(" 0x" + Integer.toHexString(evt.getKeyChar()));
|
||||
}
|
||||
|
||||
}// class CtrlASCII
|
||||
100
test/jdk/java/awt/event/KeyEvent/KeyTyped/CtrlSpace.java
Normal file
100
test/jdk/java/awt/event/KeyEvent/KeyTyped/CtrlSpace.java
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* Copyright (c) 2021, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
@test
|
||||
@key headful
|
||||
@bug 8272602
|
||||
@summary Ctrl+Space should generate a KeyTyped event on macOS
|
||||
@run main CtrlSpace
|
||||
*/
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Frame;
|
||||
import java.awt.Panel;
|
||||
import java.awt.TextField;
|
||||
import java.awt.Robot;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
|
||||
public class CtrlSpace extends Frame implements KeyListener {
|
||||
|
||||
static volatile boolean testPassed = false;
|
||||
static volatile Robot robot;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
robot = new Robot();
|
||||
robot.setAutoWaitForIdle(true);
|
||||
robot.setAutoDelay(100);
|
||||
|
||||
Frame frame = createAndShowGUI(robot);
|
||||
|
||||
test(robot);
|
||||
robot.waitForIdle();
|
||||
Thread.sleep(2000);
|
||||
frame.setVisible(false);
|
||||
frame.dispose();
|
||||
if (!testPassed) {
|
||||
throw new RuntimeException("No KeyTyped event");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static Frame createAndShowGUI(Robot robot) {
|
||||
CtrlSpace win = new CtrlSpace();
|
||||
win.setSize(300, 300);
|
||||
Panel panel = new Panel(new BorderLayout());
|
||||
TextField textField = new TextField("abcdefghijk");
|
||||
textField.addKeyListener(win);
|
||||
panel.add(textField, BorderLayout.CENTER);
|
||||
win.add(panel);
|
||||
win.setVisible(true);
|
||||
robot.waitForIdle();
|
||||
textField.requestFocusInWindow();
|
||||
robot.waitForIdle();
|
||||
return win;
|
||||
}
|
||||
|
||||
public static void test(Robot robot) {
|
||||
robot.keyPress(KeyEvent.VK_CONTROL);
|
||||
robot.keyPress(KeyEvent.VK_SPACE);
|
||||
robot.keyRelease(KeyEvent.VK_SPACE);
|
||||
robot.keyRelease(KeyEvent.VK_CONTROL);
|
||||
robot.delay(200);
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent evt) {
|
||||
System.out.println("Pressed " + evt);
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent evt) {
|
||||
System.out.println("Released " + evt);
|
||||
}
|
||||
|
||||
public void keyTyped(KeyEvent evt) {
|
||||
System.out.println("Typed " + evt);
|
||||
testPassed = true;
|
||||
}
|
||||
|
||||
}
|
||||
143
test/jdk/java/awt/event/KeyEvent/KeyTyped/DeleteKeyTyped.java
Normal file
143
test/jdk/java/awt/event/KeyEvent/KeyTyped/DeleteKeyTyped.java
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
/*
|
||||
* Copyright (c) 2002, 2023, 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.EventQueue;
|
||||
import java.awt.Frame;
|
||||
import java.awt.Robot;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4724007
|
||||
* @key headful
|
||||
* @summary Tests that KeyTyped events are fired for the Delete key
|
||||
* and that no extraneous characters are entered as a result.
|
||||
*/
|
||||
|
||||
public class DeleteKeyTyped {
|
||||
private static Frame frame;
|
||||
private static TextField tf;
|
||||
|
||||
private static boolean deleteKeyTypedReceived = false;
|
||||
private static final String ORIGINAL = "0123456789";
|
||||
private static final String SUCCESS = "123456789";
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
try {
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoWaitForIdle(true);
|
||||
robot.setAutoDelay(100);
|
||||
|
||||
EventQueue.invokeAndWait(DeleteKeyTyped::createTestUI);
|
||||
robot.waitForIdle();
|
||||
robot.delay(1000);
|
||||
|
||||
// Move cursor to start of TextField
|
||||
robot.keyPress(KeyEvent.VK_HOME);
|
||||
robot.keyRelease(KeyEvent.VK_HOME);
|
||||
robot.waitForIdle();
|
||||
robot.delay(50);
|
||||
|
||||
// Press and release Delete
|
||||
robot.keyPress(KeyEvent.VK_DELETE);
|
||||
robot.keyRelease(KeyEvent.VK_DELETE);
|
||||
robot.waitForIdle();
|
||||
robot.delay(50);
|
||||
|
||||
EventQueue.invokeAndWait(DeleteKeyTyped::testDeleteKeyEvent);
|
||||
} finally {
|
||||
EventQueue.invokeAndWait(() -> {
|
||||
if (frame != null) {
|
||||
frame.dispose();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private static void createTestUI() {
|
||||
frame = new Frame();
|
||||
tf = new TextField(ORIGINAL, 20);
|
||||
frame.add(tf);
|
||||
frame.setSize(300, 100);
|
||||
frame.setVisible(true);
|
||||
tf.requestFocusInWindow();
|
||||
|
||||
tf.addKeyListener(new KeyListener() {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
int keychar = evt.getKeyChar();
|
||||
if (keychar == 127) { // Delete character is 127 or \u007F
|
||||
deleteKeyTypedReceived = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
}
|
||||
|
||||
private void printKey(KeyEvent evt) {
|
||||
switch(evt.getID()) {
|
||||
case KeyEvent.KEY_TYPED:
|
||||
case KeyEvent.KEY_PRESSED:
|
||||
case KeyEvent.KEY_RELEASED:
|
||||
break;
|
||||
default:
|
||||
System.out.println("Other Event");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("params= " + evt.paramString() + " \n" +
|
||||
"KeyChar: " + evt.getKeyChar() + " = " + (int) evt.getKeyChar() +
|
||||
" KeyCode: " + evt.getKeyCode() +
|
||||
" Modifiers: " + evt.getModifiersEx());
|
||||
|
||||
if (evt.isActionKey()) {
|
||||
System.out.println("Action Key");
|
||||
}
|
||||
|
||||
System.out.println("keyText= " + KeyEvent.getKeyText(evt.getKeyCode()) + "\n");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void testDeleteKeyEvent() {
|
||||
if (deleteKeyTypedReceived) {
|
||||
if (tf.getText().equals(SUCCESS)) {
|
||||
System.out.println("Test PASSED");
|
||||
} else {
|
||||
System.out.println("Test FAILED: wrong string");
|
||||
throw new RuntimeException("The test failed: wrong string: " +
|
||||
tf.getText());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
136
test/jdk/java/awt/event/KeyEvent/KeyTyped/EscapeKeyTyped.java
Normal file
136
test/jdk/java/awt/event/KeyEvent/KeyTyped/EscapeKeyTyped.java
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
/*
|
||||
* Copyright (c) 2002, 2025, 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.EventQueue;
|
||||
import java.awt.Frame;
|
||||
import java.awt.Robot;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 4734408
|
||||
* @summary Tests that KeyTyped events are fired for the Escape key
|
||||
* and that no extraneous characters are entered as a result.
|
||||
*/
|
||||
|
||||
public class EscapeKeyTyped {
|
||||
private static Frame frame;
|
||||
private static TextField tf;
|
||||
|
||||
private static final String ORIGINAL = "0123456789";
|
||||
private static boolean escapeKeyTypedReceived = false;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
try {
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoWaitForIdle(true);
|
||||
robot.setAutoDelay(30);
|
||||
|
||||
EventQueue.invokeAndWait(EscapeKeyTyped::createAndShowUI);
|
||||
robot.waitForIdle();
|
||||
robot.delay(1000);
|
||||
|
||||
// Press and release Escape
|
||||
robot.keyPress(KeyEvent.VK_ESCAPE);
|
||||
robot.keyRelease(KeyEvent.VK_ESCAPE);
|
||||
robot.waitForIdle();
|
||||
robot.delay(20);
|
||||
|
||||
EventQueue.invokeAndWait(EscapeKeyTyped::testEscKeyEvent);
|
||||
} finally {
|
||||
EventQueue.invokeAndWait(() -> {
|
||||
if (frame != null) {
|
||||
frame.dispose();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private static void createAndShowUI() {
|
||||
frame = new Frame();
|
||||
tf = new TextField(ORIGINAL, 20);
|
||||
frame.add(tf);
|
||||
frame.setSize(300, 100);
|
||||
frame.setVisible(true);
|
||||
tf.requestFocusInWindow();
|
||||
|
||||
tf.addKeyListener(new KeyListener() {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
printKey(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
printKey(e);
|
||||
int keychar = e.getKeyChar();
|
||||
if (keychar == 27) { // Escape character is 27 or \u001b
|
||||
escapeKeyTypedReceived = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
printKey(e);
|
||||
}
|
||||
|
||||
private void printKey(KeyEvent evt) {
|
||||
switch (evt.getID()) {
|
||||
case KeyEvent.KEY_TYPED:
|
||||
case KeyEvent.KEY_PRESSED:
|
||||
case KeyEvent.KEY_RELEASED:
|
||||
break;
|
||||
default:
|
||||
System.out.println("Other Event");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("params= " + evt.paramString() + " \n" +
|
||||
"KeyChar: " + evt.getKeyChar() + " = " + (int) evt.getKeyChar() +
|
||||
" KeyCode: " + evt.getKeyCode() +
|
||||
" Modifiers: " + evt.getModifiersEx());
|
||||
|
||||
if (evt.isActionKey()) {
|
||||
System.out.println("Action Key");
|
||||
}
|
||||
|
||||
System.out.println("keyText= " + KeyEvent.getKeyText(evt.getKeyCode()) + "\n");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void testEscKeyEvent() {
|
||||
if (escapeKeyTypedReceived) {
|
||||
if (tf.getText().equals(ORIGINAL)) {
|
||||
System.out.println("Test PASSED");
|
||||
} else {
|
||||
System.out.println("Test FAILED: wrong string");
|
||||
throw new RuntimeException("The test failed: wrong string: " +
|
||||
tf.getText());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
178
test/jdk/java/awt/event/KeyEvent/KeyTyped/Numpad1KeyTyped.java
Normal file
178
test/jdk/java/awt/event/KeyEvent/KeyTyped/Numpad1KeyTyped.java
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
/*
|
||||
* Copyright (c) 2002, 2024, 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.Frame;
|
||||
import java.awt.Robot;
|
||||
import java.awt.TextField;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.event.FocusAdapter;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import jdk.test.lib.Platform;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4724007
|
||||
* @key headful
|
||||
* @summary Tests that KeyTyped events are fired for the Numpad1 key
|
||||
* @library /test/lib
|
||||
* @build jdk.test.lib.Platform
|
||||
* @run main Numpad1KeyTyped
|
||||
*/
|
||||
public final class Numpad1KeyTyped extends FocusAdapter implements KeyListener {
|
||||
|
||||
private static final String ORIGINAL = "0123456789";
|
||||
private static final String EXPECTED = "10123456789";
|
||||
|
||||
private final CountDownLatch typedNum1 = new CountDownLatch(1);
|
||||
private final CountDownLatch focusGained = new CountDownLatch(1);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Numpad1KeyTyped test = new Numpad1KeyTyped();
|
||||
test.start();
|
||||
}
|
||||
|
||||
private void start() throws Exception {
|
||||
Toolkit toolkit = Toolkit.getDefaultToolkit();
|
||||
Boolean oldState = null;
|
||||
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoDelay(100);
|
||||
|
||||
Frame frame = new Frame("Numpad1KeyTyped");
|
||||
TextField tf = new TextField(ORIGINAL, 20);
|
||||
frame.add(tf);
|
||||
tf.addKeyListener(this);
|
||||
|
||||
tf.addFocusListener(this);
|
||||
|
||||
frame.setSize(300, 100);
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setVisible(true);
|
||||
tf.requestFocusInWindow();
|
||||
|
||||
if (!focusGained.await(2, SECONDS)) {
|
||||
throw new RuntimeException("TextField didn't receive focus");
|
||||
}
|
||||
robot.waitForIdle();
|
||||
|
||||
try {
|
||||
// Move cursor to start of TextField
|
||||
robot.keyPress(KeyEvent.VK_HOME);
|
||||
robot.keyRelease(KeyEvent.VK_HOME);
|
||||
robot.waitForIdle();
|
||||
|
||||
if (Platform.isLinux()) {
|
||||
// Press but don't release NumLock
|
||||
robot.keyPress(KeyEvent.VK_NUM_LOCK);
|
||||
}
|
||||
if (Platform.isWindows()) {
|
||||
oldState = toolkit.getLockingKeyState(KeyEvent.VK_NUM_LOCK);
|
||||
toolkit.setLockingKeyState(KeyEvent.VK_NUM_LOCK, true);
|
||||
}
|
||||
|
||||
// Press and release Numpad-1
|
||||
robot.keyPress(KeyEvent.VK_NUMPAD1);
|
||||
robot.keyRelease(KeyEvent.VK_NUMPAD1);
|
||||
|
||||
if (!typedNum1.await(2, SECONDS)) {
|
||||
throw new RuntimeException("TextField didn't receive keyTyped('1') - too slow");
|
||||
}
|
||||
|
||||
final String text = tf.getText();
|
||||
if (!text.equals(EXPECTED)) {
|
||||
throw new RuntimeException("Test FAILED: wrong string '"
|
||||
+ text + "' vs "
|
||||
+ "expected '" + EXPECTED + "'");
|
||||
}
|
||||
System.out.println("Test PASSED");
|
||||
} finally {
|
||||
if (Platform.isLinux()) {
|
||||
// "release" + "press and release" NumLock to disable numlock
|
||||
robot.keyRelease(KeyEvent.VK_NUM_LOCK);
|
||||
robot.keyPress(KeyEvent.VK_NUM_LOCK);
|
||||
robot.keyRelease(KeyEvent.VK_NUM_LOCK);
|
||||
}
|
||||
if (oldState != null) {
|
||||
toolkit.setLockingKeyState(KeyEvent.VK_NUM_LOCK, oldState);
|
||||
}
|
||||
|
||||
frame.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void focusGained(FocusEvent e) {
|
||||
System.out.println("tf.focusGained");
|
||||
focusGained.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
|
||||
int keychar = evt.getKeyChar();
|
||||
if (keychar == '1') {
|
||||
typedNum1.countDown();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
private static void printKey(KeyEvent evt) {
|
||||
int id = evt.getID();
|
||||
if (id != KeyEvent.KEY_TYPED
|
||||
&& id != KeyEvent.KEY_PRESSED
|
||||
&& id != KeyEvent.KEY_RELEASED) {
|
||||
|
||||
System.out.println("Other Event");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("params= " + evt.paramString() + " \n" +
|
||||
"KeyChar: " + evt.getKeyChar() + " = " + (int) evt.getKeyChar() +
|
||||
" KeyCode: " + evt.getKeyCode() +
|
||||
" Modifiers: " + evt.getModifiersEx());
|
||||
|
||||
if (evt.isActionKey()) {
|
||||
System.out.println(" Action Key");
|
||||
}
|
||||
|
||||
System.out.println("keyText= " + KeyEvent.getKeyText(evt.getKeyCode()) + "\n");
|
||||
}
|
||||
|
||||
}
|
||||
119
test/jdk/java/awt/event/KeyEvent/NumpadTest.java
Normal file
119
test/jdk/java/awt/event/KeyEvent/NumpadTest.java
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4083691
|
||||
* @summary Ensures that KeyEvent has right results for the following
|
||||
* keys \*-+
|
||||
* @library /java/awt/regtesthelpers
|
||||
* @build PassFailJFrame
|
||||
* @run main/manual NumpadTest
|
||||
*/
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Frame;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
|
||||
public class NumpadTest extends Frame implements KeyListener {
|
||||
static String INSTRUCTIONS = """
|
||||
This test requires a keyboard with a numeric keypad (numpad).
|
||||
If your keyboard does not have a numpad press "Pass" to skip testing.
|
||||
Make sure NumLock is on.
|
||||
Click on the text field in the window named "Check KeyChar values".
|
||||
Then, type the following keys/characters in the TextField.
|
||||
using the numpad keys:
|
||||
/*-+
|
||||
|
||||
Verify that the keyChar and keyCode is correct for each key pressed.
|
||||
Remember that the keyCode for the KEY_TYPED event should be zero.
|
||||
Also verify that the character you typed appears in the TextField.
|
||||
|
||||
Key Name keyChar Keycode
|
||||
-------------------------------------
|
||||
/ Divide / 47 111
|
||||
* Multiply * 42 106
|
||||
- Subtract - 45 109
|
||||
+ Add + 43 107
|
||||
|
||||
Now repeat with the NumLock off.
|
||||
|
||||
If all keycodes are valid and expected characters appear
|
||||
in the text field press "Pass". Otherwise press "Fail".
|
||||
""";
|
||||
|
||||
public NumpadTest() {
|
||||
super("Check KeyChar values");
|
||||
setLayout(new BorderLayout());
|
||||
TextField tf = new TextField(30);
|
||||
tf.addKeyListener(this);
|
||||
add(tf, BorderLayout.CENTER);
|
||||
pack();
|
||||
}
|
||||
public void keyPressed(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
}
|
||||
|
||||
public void keyTyped(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
}
|
||||
|
||||
protected void printKey(KeyEvent evt) {
|
||||
switch (evt.getID()) {
|
||||
case KeyEvent.KEY_TYPED:
|
||||
break;
|
||||
case KeyEvent.KEY_PRESSED:
|
||||
break;
|
||||
case KeyEvent.KEY_RELEASED:
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
if (evt.isActionKey()) {
|
||||
PassFailJFrame.log("params= " + evt.paramString() + " KeyChar: " +
|
||||
(int) evt.getKeyChar() + " Action Key");
|
||||
} else {
|
||||
PassFailJFrame.log("params= " + evt.paramString() + " KeyChar: " +
|
||||
(int) evt.getKeyChar());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException, InvocationTargetException {
|
||||
PassFailJFrame.builder()
|
||||
.title("NumpadTest Instructions")
|
||||
.instructions(INSTRUCTIONS)
|
||||
.logArea(20)
|
||||
.testUI(NumpadTest::new)
|
||||
.build()
|
||||
.awaitAndCheck();
|
||||
}
|
||||
}
|
||||
108
test/jdk/java/awt/event/KeyEvent/NumpadTest2.java
Normal file
108
test/jdk/java/awt/event/KeyEvent/NumpadTest2.java
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4279566
|
||||
* @summary Tests that numpad keys produce the correct key codes and
|
||||
* key chars when both the NumLock and CapsLock are on.
|
||||
* @library /java/awt/regtesthelpers
|
||||
* @build PassFailJFrame
|
||||
* @run main/manual NumpadTest2
|
||||
*/
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Frame;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class NumpadTest2 extends Frame implements KeyListener {
|
||||
static String INSTRUCTIONS = """
|
||||
Make sure that the NumLock and CapsLock are both ON.
|
||||
Click on the text field inside the window named "Check KeyChar values"
|
||||
Then, type the NumPad 7 key (not the regular 7 key).
|
||||
Verify that the keyChar and keyCode is correct for each key pressed.
|
||||
Remember that the keyCode for the KEY_TYPED event should be zero.
|
||||
If 7 appears in the text field and the key code printed is correct
|
||||
press "Pass", otherwise press "Fail".
|
||||
|
||||
Key Name keyChar Keycode
|
||||
-------------------------------------------------
|
||||
Numpad-7 Numpad-7 55 103
|
||||
""";
|
||||
|
||||
public NumpadTest2() {
|
||||
super("Check KeyChar values");
|
||||
setLayout(new BorderLayout());
|
||||
TextField tf = new TextField(30);
|
||||
tf.addKeyListener(this);
|
||||
add(tf, BorderLayout.CENTER);
|
||||
pack();
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
}
|
||||
|
||||
public void keyTyped(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent evt) {
|
||||
printKey(evt);
|
||||
}
|
||||
|
||||
protected void printKey(KeyEvent evt) {
|
||||
switch (evt.getID()) {
|
||||
case KeyEvent.KEY_TYPED:
|
||||
break;
|
||||
case KeyEvent.KEY_PRESSED:
|
||||
break;
|
||||
case KeyEvent.KEY_RELEASED:
|
||||
break;
|
||||
default:
|
||||
System.out.println("Other Event ");
|
||||
return;
|
||||
}
|
||||
|
||||
if (evt.isActionKey()) {
|
||||
PassFailJFrame.log("params= " + evt.paramString() + " KeyChar: " +
|
||||
(int) evt.getKeyChar() + " Action Key");
|
||||
} else {
|
||||
PassFailJFrame.log("params= " + evt.paramString() + " KeyChar: " +
|
||||
(int) evt.getKeyChar());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException,
|
||||
InvocationTargetException {
|
||||
PassFailJFrame.builder()
|
||||
.instructions(INSTRUCTIONS)
|
||||
.logArea(10)
|
||||
.testUI(NumpadTest2::new)
|
||||
.build()
|
||||
.awaitAndCheck();
|
||||
}
|
||||
}
|
||||
184
test/jdk/java/awt/event/KeyEvent/OptionKeyEventTest.java
Normal file
184
test/jdk/java/awt/event/KeyEvent/OptionKeyEventTest.java
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
/*
|
||||
* Copyright (c) 2023, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8311922
|
||||
* @key headful
|
||||
* @library /java/awt/regtesthelpers
|
||||
* @build PassFailJFrame
|
||||
* @summary To test both option key press and release event on Mac OS
|
||||
* @run main/manual OptionKeyEventTest
|
||||
* @requires (os.family == "mac")
|
||||
*/
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
public class OptionKeyEventTest extends JFrame
|
||||
implements KeyListener, ActionListener {
|
||||
private static JTextArea displayArea;
|
||||
private static JTextField typingArea;
|
||||
private static final String newline = System.getProperty("line.separator");
|
||||
private static final String INSTRUCTIONS =
|
||||
"This test checks if the key events for the left and right\n" +
|
||||
"option keys are correct. To complete the test, ensure the\n" +
|
||||
"OptionKeyEventTest window's typing area is focused.\n" +
|
||||
"Press and release the left option key. Then press and release\n" +
|
||||
"the right option key. Confirm in the display area and pass the\n" +
|
||||
"test if these are correct, otherwise this test fails: \n\n" +
|
||||
"1. 'KEY PRESSED' appears first, followed by a 'KEY RELEASED'\n" +
|
||||
"for each option button\n" +
|
||||
"2. 'key location' shows 'left' or 'right' accordingly, not\n" +
|
||||
"'standard' or 'unknown'";
|
||||
|
||||
public static void main(String[] args)
|
||||
throws InterruptedException, InvocationTargetException {
|
||||
PassFailJFrame passFailJFrame = new PassFailJFrame("OptionKeyEventTest",
|
||||
INSTRUCTIONS, 5, 15, 35);
|
||||
createAndShowGUI();
|
||||
passFailJFrame.awaitAndCheck();
|
||||
}
|
||||
|
||||
private static void createAndShowGUI()
|
||||
throws InterruptedException, InvocationTargetException {
|
||||
SwingUtilities.invokeAndWait(() -> {
|
||||
OptionKeyEventTest frame =
|
||||
new OptionKeyEventTest("OptionKeyEventTest");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.addComponentsToPane();
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
PassFailJFrame.addTestWindow(frame);
|
||||
PassFailJFrame.positionTestWindow(frame,
|
||||
PassFailJFrame.Position.HORIZONTAL);
|
||||
typingArea.requestFocus();
|
||||
});
|
||||
}
|
||||
|
||||
private void addComponentsToPane() {
|
||||
JButton button = new JButton("Clear");
|
||||
button.addActionListener(this);
|
||||
|
||||
typingArea = new JTextField(20);
|
||||
typingArea.addKeyListener(this);
|
||||
|
||||
displayArea = new JTextArea();
|
||||
displayArea.setEditable(false);
|
||||
displayArea.setFocusable(false);
|
||||
JScrollPane scrollPane = new JScrollPane(displayArea);
|
||||
scrollPane.setPreferredSize(new Dimension(375, 125));
|
||||
|
||||
getContentPane().add(typingArea, BorderLayout.PAGE_START);
|
||||
getContentPane().add(scrollPane, BorderLayout.CENTER);
|
||||
getContentPane().add(button, BorderLayout.PAGE_END);
|
||||
}
|
||||
|
||||
public OptionKeyEventTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public void keyTyped(KeyEvent e) {
|
||||
displayInfo(e, "KEY TYPED: ");
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
displayInfo(e, "KEY PRESSED: ");
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
displayInfo(e, "KEY RELEASED: ");
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
//Clear the text components.
|
||||
displayArea.setText("");
|
||||
typingArea.setText("");
|
||||
|
||||
typingArea.requestFocusInWindow();
|
||||
}
|
||||
|
||||
private void displayInfo(KeyEvent e, String keyStatus) {
|
||||
int id = e.getID();
|
||||
String keyString;
|
||||
if (id == KeyEvent.KEY_TYPED) {
|
||||
char c = e.getKeyChar();
|
||||
keyString = "key character = '" + c + "'";
|
||||
} else {
|
||||
int keyCode = e.getKeyCode();
|
||||
keyString = "key code = " + keyCode
|
||||
+ " ("
|
||||
+ KeyEvent.getKeyText(keyCode)
|
||||
+ ")";
|
||||
}
|
||||
|
||||
int modifiersEx = e.getModifiersEx();
|
||||
String modString = "extended modifiers = " + modifiersEx;
|
||||
String tmpString = KeyEvent.getModifiersExText(modifiersEx);
|
||||
if (tmpString.length() > 0) {
|
||||
modString += " (" + tmpString + ")";
|
||||
} else {
|
||||
modString += " (no extended modifiers)";
|
||||
}
|
||||
|
||||
String actionString = "action key? ";
|
||||
if (e.isActionKey()) {
|
||||
actionString += "YES";
|
||||
} else {
|
||||
actionString += "NO";
|
||||
}
|
||||
|
||||
String locationString = "key location: ";
|
||||
int location = e.getKeyLocation();
|
||||
if (location == KeyEvent.KEY_LOCATION_STANDARD) {
|
||||
locationString += "standard";
|
||||
} else if (location == KeyEvent.KEY_LOCATION_LEFT) {
|
||||
locationString += "left";
|
||||
} else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
|
||||
locationString += "right";
|
||||
} else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
|
||||
locationString += "numpad";
|
||||
} else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
|
||||
locationString += "unknown";
|
||||
}
|
||||
|
||||
displayArea.append(keyStatus + newline
|
||||
+ " " + keyString + newline
|
||||
+ " " + modString + newline
|
||||
+ " " + actionString + newline
|
||||
+ " " + locationString + newline);
|
||||
displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,146 @@
|
|||
/*
|
||||
* Copyright (c) 2015, 2024, 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.AWTKeyStroke;
|
||||
import java.awt.event.InputEvent;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInput;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutput;
|
||||
import java.io.ObjectOutputStream;
|
||||
import javax.swing.KeyStroke;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8133453
|
||||
* @summary Remove AWTKeyStroke.registerSubclass(Class) method
|
||||
* @run main TestAWTKeyStroke
|
||||
*/
|
||||
public class TestAWTKeyStroke {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
int modifiers = InputEvent.CTRL_MASK | InputEvent.CTRL_DOWN_MASK;
|
||||
checkAWTKeyStroke('A', modifiers, true);
|
||||
checkKeyStroke('B', modifiers, false);
|
||||
checkAWTKeyStroke('C', modifiers, false);
|
||||
checkKeyStroke('D', modifiers, true);
|
||||
checkSerializedKeyStrokes('E', modifiers, true);
|
||||
}
|
||||
|
||||
private static void checkAWTKeyStroke(int keyCode, int modifiers,
|
||||
boolean onKeyRelease) throws Exception {
|
||||
|
||||
AWTKeyStroke awtKeyStroke1 = AWTKeyStroke.getAWTKeyStroke(
|
||||
keyCode, modifiers, onKeyRelease);
|
||||
|
||||
checkAWTKeyStroke(awtKeyStroke1, keyCode, modifiers, onKeyRelease);
|
||||
|
||||
AWTKeyStroke awtKeyStroke2 = AWTKeyStroke.getAWTKeyStroke(
|
||||
keyCode, modifiers, onKeyRelease);
|
||||
|
||||
if (awtKeyStroke1 != awtKeyStroke2) {
|
||||
throw new RuntimeException("AWTKeyStroke is not cached!");
|
||||
}
|
||||
|
||||
checkSerializedKeyStroke(awtKeyStroke1);
|
||||
}
|
||||
|
||||
private static void checkKeyStroke(int keyCode, int modifiers,
|
||||
boolean onKeyRelease) throws Exception {
|
||||
|
||||
KeyStroke keyStroke1 = KeyStroke.getKeyStroke(
|
||||
keyCode, modifiers, onKeyRelease);
|
||||
checkAWTKeyStroke(keyStroke1, keyCode, modifiers, onKeyRelease);
|
||||
|
||||
KeyStroke keyStroke2 = KeyStroke.getKeyStroke(
|
||||
keyCode, modifiers, onKeyRelease);
|
||||
|
||||
if (keyStroke1 != keyStroke2) {
|
||||
throw new RuntimeException("KeyStroke is not cached!");
|
||||
}
|
||||
|
||||
checkSerializedKeyStroke(keyStroke1);
|
||||
}
|
||||
|
||||
private static void checkSerializedKeyStrokes(int keyCode, int modifiers,
|
||||
boolean onKeyRelease) throws Exception {
|
||||
|
||||
AWTKeyStroke awtKeyStroke = AWTKeyStroke.getAWTKeyStroke(
|
||||
keyCode, modifiers, onKeyRelease);
|
||||
|
||||
KeyStroke keyStroke = KeyStroke.getKeyStroke(
|
||||
keyCode, modifiers, onKeyRelease);
|
||||
|
||||
if (awtKeyStroke != getSerializedAWTKeyStroke(awtKeyStroke)) {
|
||||
throw new RuntimeException("Serialized AWTKeyStroke is not cached!");
|
||||
}
|
||||
|
||||
awtKeyStroke = AWTKeyStroke.getAWTKeyStroke(
|
||||
keyCode, modifiers, !onKeyRelease);
|
||||
|
||||
if (!keyStroke.equals(getSerializedAWTKeyStroke(keyStroke))) {
|
||||
throw new RuntimeException("Serialized KeyStroke is not cached!");
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkAWTKeyStroke(AWTKeyStroke awtKeyStroke,
|
||||
int keyCode, int modifiers, boolean onKeyRelease) {
|
||||
|
||||
if (awtKeyStroke.getKeyCode() != keyCode) {
|
||||
throw new RuntimeException("Wrong key code!");
|
||||
}
|
||||
|
||||
if (awtKeyStroke.getModifiers() != modifiers) {
|
||||
throw new RuntimeException("Wrong modifiers!");
|
||||
}
|
||||
|
||||
if (awtKeyStroke.isOnKeyRelease() != onKeyRelease) {
|
||||
throw new RuntimeException("Wrong on key release!");
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkSerializedKeyStroke(AWTKeyStroke keyStroke)
|
||||
throws Exception {
|
||||
if (keyStroke != getSerializedAWTKeyStroke(keyStroke)) {
|
||||
throw new RuntimeException("New instance is returned during"
|
||||
+ " serialization!");
|
||||
}
|
||||
}
|
||||
|
||||
private static AWTKeyStroke getSerializedAWTKeyStroke(AWTKeyStroke keyStroke)
|
||||
throws Exception {
|
||||
|
||||
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
ObjectOutput out = new ObjectOutputStream(bos)) {
|
||||
out.writeObject(keyStroke);
|
||||
byte[] bytes = bos.toByteArray();
|
||||
|
||||
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
||||
ObjectInput in = new ObjectInputStream(bis)) {
|
||||
return (AWTKeyStroke) in.readObject();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
92
test/jdk/java/awt/event/KeyEvent/RobotCrash/RobotCrash.java
Normal file
92
test/jdk/java/awt/event/KeyEvent/RobotCrash/RobotCrash.java
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
/*
|
||||
* Copyright (c) 2016, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 8165555
|
||||
* @summary VM crash after creating Robot second time and accessing key codes in
|
||||
* single JVM mode.
|
||||
* @run main RobotCrash
|
||||
*/
|
||||
import java.awt.Frame;
|
||||
import java.awt.Point;
|
||||
import java.awt.Robot;
|
||||
import java.awt.event.InputEvent;
|
||||
import java.awt.event.KeyEvent;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
public class RobotCrash implements Runnable {
|
||||
|
||||
private Frame frame;
|
||||
|
||||
public void robotKeyPressTest() throws Exception {
|
||||
|
||||
SwingUtilities.invokeAndWait(() -> {
|
||||
frame = new Frame();
|
||||
frame.setSize(300, 300);
|
||||
frame.setVisible(true);
|
||||
});
|
||||
|
||||
Robot robot = new Robot();
|
||||
robot.waitForIdle();
|
||||
Point pt = frame.getLocationOnScreen();
|
||||
robot.mouseMove(((int) pt.getX() + frame.getWidth()) / 2,
|
||||
((int) pt.getY() + frame.getHeight()) / 2);
|
||||
robot.waitForIdle();
|
||||
robot.mousePress(InputEvent.BUTTON1_MASK);
|
||||
robot.waitForIdle();
|
||||
robot.mouseRelease(InputEvent.BUTTON1_MASK);
|
||||
robot.waitForIdle();
|
||||
robot.keyPress(KeyEvent.VK_ENTER);
|
||||
robot.waitForIdle();
|
||||
robot.keyRelease(KeyEvent.VK_ENTER);
|
||||
robot.waitForIdle();
|
||||
|
||||
SwingUtilities.invokeAndWait(() -> {
|
||||
frame.dispose();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
robotKeyPressTest();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Test Failed" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Thread t1 = new Thread(new RobotCrash());
|
||||
t1.start();
|
||||
t1.join();
|
||||
Thread t2 = new Thread(new RobotCrash());
|
||||
t2.start();
|
||||
t2.join();
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
68
test/jdk/java/awt/event/KeyEvent/ShiftF10Test.java
Normal file
68
test/jdk/java/awt/event/KeyEvent/ShiftF10Test.java
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Copyright (c) 2004, 2023, 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.EventQueue;
|
||||
import java.awt.Frame;
|
||||
import java.awt.Robot;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 4965227
|
||||
* @requires (os.family == "linux")
|
||||
* @summary tests that Shift+F10 during Window show doesn't cause deadlock- Linux only
|
||||
*/
|
||||
|
||||
public class ShiftF10Test {
|
||||
private static Frame frame;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
try {
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoDelay(10);
|
||||
|
||||
EventQueue.invokeLater(() -> {
|
||||
frame = new Frame("Deadlocking one");
|
||||
frame.setSize(100, 100);
|
||||
frame.setVisible(true);
|
||||
});
|
||||
|
||||
for (int i = 0; i < 250; i++) {
|
||||
robot.keyPress(KeyEvent.VK_SHIFT);
|
||||
robot.keyPress(KeyEvent.VK_F10);
|
||||
robot.keyRelease(KeyEvent.VK_F10);
|
||||
robot.keyRelease(KeyEvent.VK_SHIFT);
|
||||
robot.delay(10);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Test Failed due to following error: ", e);
|
||||
} finally {
|
||||
EventQueue.invokeAndWait(() -> {
|
||||
if (frame != null) {
|
||||
frame.dispose();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2024, 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 jdk.test.lib.Platform;
|
||||
import java.awt.AWTException;
|
||||
import java.awt.Frame;
|
||||
import java.awt.Robot;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import test.java.awt.regtesthelpers.Util;
|
||||
|
||||
/*
|
||||
@test
|
||||
@key headful
|
||||
@bug 7154072 7161320
|
||||
@summary Tests that key events with modifiers are not swallowed.
|
||||
@requires (os.family != "windows")
|
||||
@library /java/awt/regtesthelpers
|
||||
@library /test/lib
|
||||
@modules java.desktop/sun.awt
|
||||
@build jdk.test.lib.Platform
|
||||
@build Util
|
||||
@run main SwallowKeyEvents
|
||||
*/
|
||||
|
||||
public class SwallowKeyEvents {
|
||||
static final int PRESS_COUNT = 10;
|
||||
|
||||
static int keyPressedCount = 0;
|
||||
|
||||
static Frame f = new Frame("Frame");
|
||||
static TextField t = new TextField("text");
|
||||
static Robot r;
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (Platform.isWindows()) {
|
||||
System.out.println("Skipped. Test not for MS Windows.");
|
||||
return;
|
||||
}
|
||||
|
||||
f.add(t);
|
||||
f.pack();
|
||||
f.setVisible(true);
|
||||
|
||||
t.requestFocus();
|
||||
|
||||
try {
|
||||
r = new Robot();
|
||||
} catch (AWTException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
|
||||
Util.waitForIdle(r);
|
||||
|
||||
t.addKeyListener(new KeyAdapter() {
|
||||
public void keyPressed(KeyEvent ke) {
|
||||
System.out.println(ke);
|
||||
if (ke.getKeyCode() == KeyEvent.VK_M) {
|
||||
keyPressedCount++;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test();
|
||||
r.waitForIdle();
|
||||
r.delay(500);
|
||||
|
||||
System.out.println("key_pressed count: " + keyPressedCount);
|
||||
|
||||
if (keyPressedCount != PRESS_COUNT) {
|
||||
throw new RuntimeException("Test failed!");
|
||||
} else {
|
||||
System.out.println("Test passed.");
|
||||
}
|
||||
}
|
||||
|
||||
public static void test() {
|
||||
r.keyPress(KeyEvent.VK_SHIFT);
|
||||
r.keyPress(KeyEvent.VK_META);
|
||||
|
||||
for (int i=0; i<PRESS_COUNT; i++) {
|
||||
r.delay(100);
|
||||
r.keyPress(KeyEvent.VK_M);
|
||||
r.delay(100);
|
||||
r.keyRelease(KeyEvent.VK_M);
|
||||
}
|
||||
|
||||
r.keyRelease(KeyEvent.VK_META);
|
||||
r.keyRelease(KeyEvent.VK_SHIFT);
|
||||
}
|
||||
}
|
||||
87
test/jdk/java/awt/event/KeyEvent/TestDoubleKeyEvent.java
Normal file
87
test/jdk/java/awt/event/KeyEvent/TestDoubleKeyEvent.java
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4495473
|
||||
* @summary Tests that when you press key on canvas-type heavyweight only one key event arrives
|
||||
* @key headful
|
||||
* @run main TestDoubleKeyEvent
|
||||
*/
|
||||
|
||||
import java.awt.AWTException;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.Robot;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JWindow;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
public class TestDoubleKeyEvent extends JFrame {
|
||||
JWindow w;
|
||||
JTextField tf;
|
||||
|
||||
public void initUI() {
|
||||
setLayout(new BorderLayout());
|
||||
setTitle("Double Key Event Test");
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
w = new JWindow(this);
|
||||
w.setLayout(new FlowLayout());
|
||||
tf = new JTextField(20);
|
||||
w.add(tf);
|
||||
w.pack();
|
||||
w.setLocationRelativeTo(null);
|
||||
w.setVisible(true);
|
||||
tf.requestFocus();
|
||||
}
|
||||
|
||||
public void testAndClean() {
|
||||
String str = tf.getText();
|
||||
w.dispose();
|
||||
dispose();
|
||||
if (str.length() != str.chars().distinct().count()) {
|
||||
throw new RuntimeException("Duplicate characters found!");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException,
|
||||
InvocationTargetException, AWTException {
|
||||
TestDoubleKeyEvent test = new TestDoubleKeyEvent();
|
||||
EventQueue.invokeAndWait(test::initUI);
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoDelay(50);
|
||||
robot.waitForIdle();
|
||||
robot.delay(1000);
|
||||
for (int i = 0; i < 15; i++) {
|
||||
robot.keyPress(KeyEvent.VK_A + i);
|
||||
robot.keyRelease(KeyEvent.VK_A + i);
|
||||
robot.waitForIdle();
|
||||
}
|
||||
robot.delay(1000);
|
||||
EventQueue.invokeAndWait(test::testAndClean);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue