1
1
/*
2
- * Copyright (c) 2004, 2022 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2004, 2025 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
23
23
24
24
/*
25
25
@test
26
- @bug 5013984
26
+ @bug 5013984 8360647
27
27
@summary Tests KEY_PRESSED has the same KeyChar as KEY_RELEASED
28
28
@key headful
29
29
@run main KeyCharTest
37
37
import java .util .HashMap ;
38
38
39
39
public class KeyCharTest extends Frame implements KeyListener {
40
- HashMap <Integer , Character > transMap = new HashMap ();
40
+ HashMap <Integer , Character > transMap = new HashMap <> ();
41
41
42
42
public void keyTyped (KeyEvent e ){
43
43
}
@@ -47,22 +47,35 @@ public void keyPressed(KeyEvent e){
47
47
}
48
48
49
49
public void keyReleased (KeyEvent e ){
50
- Object value = transMap .get (e .getKeyCode ());
51
- if (value != null && e .getKeyChar () != (( Character ) value ). charValue () ) {
50
+ Character value = transMap .get (e .getKeyCode ());
51
+ if (value != null && e .getKeyChar () != value ) {
52
52
throw new RuntimeException ("Wrong KeyChar on KEY_RELEASED " +
53
53
KeyEvent .getKeyText (e .getKeyCode ()));
54
54
}
55
55
}
56
56
57
- public void start () {
57
+ private void testKeyRange (Robot robot , int start , int end ) {
58
+ System .out .printf ("\n Testing range on %d to %d\n " , start , end );
59
+ for (int vkey = start ; vkey <= end ; vkey ++) {
60
+ try {
61
+ robot .keyPress (vkey );
62
+ robot .keyRelease (vkey );
63
+ System .out .println (KeyEvent .getKeyText (vkey ) + " " + vkey );
64
+ } catch (RuntimeException ignored ) {}
65
+ }
66
+ robot .delay (100 );
67
+ }
68
+
69
+ public void start () throws Exception {
70
+ Robot robot = new Robot ();
58
71
addKeyListener (this );
59
72
setLocationRelativeTo (null );
60
73
setSize (200 , 200 );
61
74
setVisible (true );
62
75
requestFocus ();
63
76
77
+ boolean wasNumlockPressed = false ;
64
78
try {
65
- Robot robot = new Robot ();
66
79
robot .setAutoDelay (10 );
67
80
robot .setAutoWaitForIdle (true );
68
81
robot .delay (100 );
@@ -72,22 +85,25 @@ public void start () {
72
85
robot .mousePress (MouseEvent .BUTTON1_DOWN_MASK );
73
86
robot .mouseRelease (MouseEvent .BUTTON1_DOWN_MASK );
74
87
75
- for (int vkey = 0x20 ; vkey < 0x7F ; vkey ++) {
76
- try {
77
- robot .keyPress (vkey );
78
- robot .keyRelease (vkey );
79
- System .out .println (KeyEvent .getKeyText (vkey ) + " " + vkey );
80
- } catch (RuntimeException e ) {
81
- }
82
- }
83
- robot .delay (100 );
88
+ testKeyRange (robot , 0x20 , 0x7E );
89
+
90
+ // Try again with a different numpad state.
91
+ robot .keyPress (KeyEvent .VK_NUM_LOCK );
92
+ robot .keyRelease (KeyEvent .VK_NUM_LOCK );
93
+ wasNumlockPressed = true ;
94
+
95
+ testKeyRange (robot , KeyEvent .VK_NUMPAD0 , KeyEvent .VK_DIVIDE );
84
96
} catch (Exception e ){
85
- e .printStackTrace ();
86
97
throw new RuntimeException ("Exception while performing Robot actions." );
98
+ } finally {
99
+ if (wasNumlockPressed ) {
100
+ robot .keyPress (KeyEvent .VK_NUM_LOCK );
101
+ robot .keyRelease (KeyEvent .VK_NUM_LOCK );
102
+ }
87
103
}
88
104
}
89
105
90
- public static void main (String [] args ) {
106
+ public static void main (String [] args ) throws Exception {
91
107
KeyCharTest test = new KeyCharTest ();
92
108
try {
93
109
test .start ();
0 commit comments