11/*
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.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2323
2424/*
2525 @test
26- @bug 5013984
26+ @bug 5013984 8360647
2727 @summary Tests KEY_PRESSED has the same KeyChar as KEY_RELEASED
2828 @key headful
2929 @run main KeyCharTest
3737import java .util .HashMap ;
3838
3939public class KeyCharTest extends Frame implements KeyListener {
40- HashMap <Integer , Character > transMap = new HashMap ();
40+ HashMap <Integer , Character > transMap = new HashMap <> ();
4141
4242 public void keyTyped (KeyEvent e ){
4343 }
@@ -47,22 +47,35 @@ public void keyPressed(KeyEvent e){
4747 }
4848
4949 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 ) {
5252 throw new RuntimeException ("Wrong KeyChar on KEY_RELEASED " +
5353 KeyEvent .getKeyText (e .getKeyCode ()));
5454 }
5555 }
5656
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 ();
5871 addKeyListener (this );
5972 setLocationRelativeTo (null );
6073 setSize (200 , 200 );
6174 setVisible (true );
6275 requestFocus ();
6376
77+ boolean wasNumlockPressed = false ;
6478 try {
65- Robot robot = new Robot ();
6679 robot .setAutoDelay (10 );
6780 robot .setAutoWaitForIdle (true );
6881 robot .delay (100 );
@@ -72,22 +85,25 @@ public void start () {
7285 robot .mousePress (MouseEvent .BUTTON1_DOWN_MASK );
7386 robot .mouseRelease (MouseEvent .BUTTON1_DOWN_MASK );
7487
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 );
8496 } catch (Exception e ){
85- e .printStackTrace ();
8697 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+ }
87103 }
88104 }
89105
90- public static void main (String [] args ) {
106+ public static void main (String [] args ) throws Exception {
91107 KeyCharTest test = new KeyCharTest ();
92108 try {
93109 test .start ();
0 commit comments