Skip to content

Commit 65abe4f

Browse files
author
Satyen Subramaniam
committed
8354106: Clean up and open source KeyEvent related tests (Part 2)
Backport-of: e163a76f2bacf06980026feb7e645e616ffe2ad4
1 parent f8c20f8 commit 65abe4f

File tree

2 files changed

+212
-0
lines changed

2 files changed

+212
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 4174399
27+
* @summary Check that modifier values are set on a KeyPressed event
28+
* when a modifier key is pressed.
29+
* @key headful
30+
* @run main KeyPressedModifiers
31+
*/
32+
33+
import java.awt.AWTException;
34+
import java.awt.BorderLayout;
35+
import java.awt.EventQueue;
36+
import java.awt.Frame;
37+
import java.awt.Robot;
38+
import java.awt.TextField;
39+
import java.awt.event.KeyEvent;
40+
import java.awt.event.KeyListener;
41+
import java.lang.reflect.InvocationTargetException;
42+
import java.util.concurrent.atomic.AtomicBoolean;
43+
44+
public class KeyPressedModifiers extends Frame implements KeyListener {
45+
static AtomicBoolean shiftDown = new AtomicBoolean(false);
46+
static AtomicBoolean controlDown = new AtomicBoolean(false);
47+
static AtomicBoolean altDown = new AtomicBoolean(false);
48+
49+
public static void main(String[] args) throws InterruptedException,
50+
InvocationTargetException, AWTException {
51+
KeyPressedModifiers test = new KeyPressedModifiers();
52+
try {
53+
EventQueue.invokeAndWait(test::initUI);
54+
Robot robot = new Robot();
55+
robot.setAutoDelay(100);
56+
robot.delay(500);
57+
robot.waitForIdle();
58+
robot.keyPress(KeyEvent.VK_SHIFT);
59+
robot.keyRelease(KeyEvent.VK_SHIFT);
60+
robot.keyPress(KeyEvent.VK_CONTROL);
61+
robot.keyRelease(KeyEvent.VK_CONTROL);
62+
robot.keyPress(KeyEvent.VK_ALT);
63+
robot.keyRelease(KeyEvent.VK_ALT);
64+
robot.delay(500);
65+
robot.waitForIdle();
66+
if (!shiftDown.get() || !controlDown.get() || !altDown.get()) {
67+
String error = "Following key modifiers were not registered:" +
68+
(shiftDown.get() ? "" : " SHIFT") +
69+
(controlDown.get() ? "" : " CONTROL") +
70+
(altDown.get() ? "" : " ALT");
71+
throw new RuntimeException(error);
72+
}
73+
} finally {
74+
EventQueue.invokeAndWait(test::dispose);
75+
}
76+
}
77+
78+
public void initUI() {
79+
setLayout(new BorderLayout());
80+
TextField tf = new TextField(30);
81+
tf.addKeyListener(this);
82+
add(tf, BorderLayout.CENTER);
83+
setSize(350, 100);
84+
setVisible(true);
85+
tf.requestFocus();
86+
}
87+
88+
public void keyTyped(KeyEvent ignore) {
89+
}
90+
91+
public void keyReleased(KeyEvent ignore) {
92+
}
93+
94+
public void keyPressed(KeyEvent e) {
95+
System.out.println(e);
96+
switch (e.getKeyCode()) {
97+
case KeyEvent.VK_SHIFT:
98+
shiftDown.set(e.isShiftDown());
99+
break;
100+
case KeyEvent.VK_CONTROL:
101+
controlDown.set(e.isControlDown());
102+
break;
103+
case KeyEvent.VK_ALT:
104+
altDown.set(e.isAltDown());
105+
break;
106+
}
107+
}
108+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 4151419 4090870 4169733
27+
* @summary Ensures that KeyEvent has right results for the following
28+
* keys -=\[];,./
29+
* @library /java/awt/regtesthelpers
30+
* @build PassFailJFrame
31+
* @run main/manual KeyTest
32+
*/
33+
34+
import java.awt.BorderLayout;
35+
import java.awt.Frame;
36+
import java.awt.TextField;
37+
import java.awt.event.KeyEvent;
38+
import java.awt.event.KeyListener;
39+
import java.lang.reflect.InvocationTargetException;
40+
41+
public class KeyTest extends Frame implements KeyListener {
42+
43+
static String INSTRUCTIONS = """
44+
Click on the text field in window named "Check KeyChar values"
45+
Type the following keys/characters in the TextField:
46+
- = \\ [ ] ; , . /
47+
Verify that the keyChar and keyCode is correct for each key pressed.
48+
Remember that the keyCode for the KEY_TYPED event should be zero.
49+
Also verify that the character you typed appears in the TextField.
50+
51+
Key Name keyChar Keycode
52+
-------------------------------------
53+
- Minus - 45 45
54+
= Equals = 61 61
55+
\\ Slash \\ 92 92
56+
[ Left Brace [ 91 91
57+
] Right Brace ] 93 93
58+
; Semicolon ; 59 59
59+
, Comma , 44 44
60+
. Period . 46 46
61+
/ Front Slash / 47 47
62+
""";
63+
public KeyTest() {
64+
super("Check KeyChar values");
65+
setLayout(new BorderLayout());
66+
TextField tf = new TextField(30);
67+
tf.addKeyListener(this);
68+
add(tf, BorderLayout.CENTER);
69+
pack();
70+
71+
}
72+
73+
public void keyPressed(KeyEvent evt) {
74+
printKey(evt);
75+
}
76+
77+
public void keyTyped(KeyEvent evt) {
78+
printKey(evt);
79+
}
80+
81+
public void keyReleased(KeyEvent evt) {
82+
printKey(evt);
83+
}
84+
85+
protected void printKey(KeyEvent evt) {
86+
if (evt.isActionKey()) {
87+
PassFailJFrame.log("params= " + evt.paramString() + " KeyChar: " +
88+
(int) evt.getKeyChar() + " Action Key");
89+
} else {
90+
PassFailJFrame.log("params= " + evt.paramString() + " KeyChar: " +
91+
(int) evt.getKeyChar());
92+
}
93+
}
94+
95+
public static void main(String[] args) throws InterruptedException, InvocationTargetException {
96+
PassFailJFrame.builder()
97+
.title("KeyTest Instructions")
98+
.instructions(INSTRUCTIONS)
99+
.logArea(20)
100+
.testUI(KeyTest::new)
101+
.build()
102+
.awaitAndCheck();
103+
}
104+
}

0 commit comments

Comments
 (0)