8315701: [macos] Regression: KeyEvent has different keycode on different keyboard layouts

Reviewed-by: aivanov, kizune
This commit is contained in:
Alisen Chung 2023-11-30 16:11:09 +00:00
parent 6aba6aa6f1
commit a3eb664077
7 changed files with 161 additions and 334 deletions
src/java.desktop/macosx
test/jdk/java/awt/event/KeyEvent/AcceleratorTest

@ -1044,7 +1044,7 @@ public class LWWindowPeer
*/
@Override
public void notifyKeyEvent(int id, long when, int modifiers,
int keyCode, char keyChar, int keyLocation)
int keyCode, char keyChar, int keyLocation, int extendedKeyCode)
{
LWKeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
Component focusOwner = kfmPeer.getCurrentFocusOwner();
@ -1058,9 +1058,13 @@ public class LWWindowPeer
KeyEvent keyEvent = new KeyEvent(focusOwner, id, when, modifiers,
keyCode, keyChar, keyLocation);
AWTAccessor.getKeyEventAccessor().setExtendedKeyCode(keyEvent,
(keyChar == KeyEvent.CHAR_UNDEFINED) ? keyCode
: ExtendedKeyCodes.getExtendedKeyCodeForChar(keyChar));
if (extendedKeyCode >= 0) {
AWTAccessor.getKeyEventAccessor().setExtendedKeyCode(keyEvent, extendedKeyCode);
} else {
AWTAccessor.getKeyEventAccessor().setExtendedKeyCode(keyEvent,
(keyChar == KeyEvent.CHAR_UNDEFINED) ? keyCode
: ExtendedKeyCodes.getExtendedKeyCodeForChar(keyChar));
}
postEvent(keyEvent);
}

@ -61,5 +61,5 @@ public interface PlatformEventNotifier {
* Called by the delegate when a key is pressed.
*/
void notifyKeyEvent(int id, long when, int modifiers,
int keyCode, char keyChar, int keyLocation);
int keyCode, char keyChar, int keyLocation, int extendedKeyCode);
}

@ -143,6 +143,7 @@ final class CPlatformResponder {
int jeventType = KeyEvent.KEY_PRESSED;
int jkeyCode = KeyEvent.VK_UNDEFINED;
int jextendedkeyCode = -1;
int jkeyLocation = KeyEvent.KEY_LOCATION_UNKNOWN;
boolean postsTyped = false;
boolean spaceKeyTyped = false;
@ -173,7 +174,7 @@ final class CPlatformResponder {
charsIgnoringModifiers.charAt(0) : KeyEvent.CHAR_UNDEFINED;
int[] in = new int[] {testCharIgnoringModifiers, isDeadChar ? 1 : 0, modifierFlags, keyCode};
int[] out = new int[3]; // [jkeyCode, jkeyLocation, deadChar]
int[] out = new int[4]; // [jkeyCode, jkeyLocation, deadChar, extendedKeyCode]
postsTyped = NSEvent.nsToJavaKeyInfo(in, out);
if (!postsTyped) {
@ -201,6 +202,7 @@ final class CPlatformResponder {
}
jkeyCode = out[0];
jextendedkeyCode = out[3];
jkeyLocation = out[1];
jeventType = isNpapiCallback ? NSEvent.npToJavaEventType(eventType) :
NSEvent.nsToJavaEventType(eventType);
@ -221,7 +223,7 @@ final class CPlatformResponder {
lastKeyPressCode = jkeyCode;
}
eventNotifier.notifyKeyEvent(jeventType, when, jmodifiers,
jkeyCode, javaChar, jkeyLocation);
jkeyCode, javaChar, jkeyLocation, jextendedkeyCode);
// Current browser may be sending input events, so don't
// post the KEY_TYPED here.
@ -241,12 +243,12 @@ final class CPlatformResponder {
}
eventNotifier.notifyKeyEvent(KeyEvent.KEY_TYPED, when, jmodifiers,
KeyEvent.VK_UNDEFINED, javaChar,
KeyEvent.KEY_LOCATION_UNKNOWN);
KeyEvent.KEY_LOCATION_UNKNOWN, jextendedkeyCode);
//If events come from Firefox, released events should also be generated.
if (needsKeyReleased) {
eventNotifier.notifyKeyEvent(KeyEvent.KEY_RELEASED, when, jmodifiers,
jkeyCode, javaChar,
KeyEvent.KEY_LOCATION_UNKNOWN);
KeyEvent.KEY_LOCATION_UNKNOWN, jextendedkeyCode);
}
}
}
@ -260,13 +262,13 @@ final class CPlatformResponder {
eventNotifier.notifyKeyEvent(KeyEvent.KEY_TYPED,
System.currentTimeMillis(),
0, KeyEvent.VK_UNDEFINED, c,
KeyEvent.KEY_LOCATION_UNKNOWN);
KeyEvent.KEY_LOCATION_UNKNOWN, -1);
index++;
}
eventNotifier.notifyKeyEvent(KeyEvent.KEY_RELEASED,
System.currentTimeMillis(),
0, lastKeyPressCode, c,
KeyEvent.KEY_LOCATION_UNKNOWN);
KeyEvent.KEY_LOCATION_UNKNOWN, -1);
}
}

@ -245,7 +245,7 @@ public final class CWarningWindow extends CPlatformWindow
@Override
public void notifyKeyEvent(int id, long when, int modifiers, int keyCode,
char keyChar, int keyLocation) {
char keyChar, int keyLocation, int jextendedkeyCode) {
}
protected int getInitialStyleBits() {

@ -429,7 +429,7 @@ static void
NsCharToJavaVirtualKeyCode(unichar ch, BOOL isDeadChar,
NSUInteger flags, unsigned short key,
jint *keyCode, jint *keyLocation, BOOL *postsTyped,
unichar *deadChar)
unichar *deadChar, jint *extendedkeyCode)
{
static size_t size = sizeof(keyTable) / sizeof(struct _key);
NSInteger offset;
@ -469,10 +469,9 @@ NsCharToJavaVirtualKeyCode(unichar ch, BOOL isDeadChar,
*postsTyped = YES;
// do quick conversion
// the keyCode is off by 32, so adding it here
*keyCode = java_awt_event_KeyEvent_VK_A + offset + 32;
*extendedkeyCode = java_awt_event_KeyEvent_VK_A + offset + 32;
*keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_STANDARD;
return;
}
}
}
if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:ch]) {
@ -695,18 +694,20 @@ JNI_COCOA_ENTER(env);
jshort keyCode = (jshort)data[3];
jint jkeyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
jint jextendedkeyCode = -1;
jint jkeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
jint testDeadChar = 0;
NsCharToJavaVirtualKeyCode((unichar)testChar, isDeadChar,
(NSUInteger)modifierFlags, (unsigned short)keyCode,
&jkeyCode, &jkeyLocation, &postsTyped,
(unichar *) &testDeadChar);
(unichar *) &testDeadChar, &jextendedkeyCode);
// out = [jkeyCode, jkeyLocation, deadChar];
// out = [jkeyCode, jkeyLocation, deadChar, jextendedkeyCode];
(*env)->SetIntArrayRegion(env, outData, 0, 1, &jkeyCode);
(*env)->SetIntArrayRegion(env, outData, 1, 1, &jkeyLocation);
(*env)->SetIntArrayRegion(env, outData, 2, 1, &testDeadChar);
(*env)->SetIntArrayRegion(env, outData, 3, 1, &jextendedkeyCode);
(*env)->ReleaseIntArrayElements(env, inData, data, 0);

@ -1,43 +0,0 @@
<!--
Copyright (c) 2009, 2013, 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.
-->
<html>
<!--
@test
@bug 6680988
@summary verify that various shortcuts and accelerators work
@author yuri.nesterenko : area=awt.keyboard
@run applet/manual=yesno AcceleratorTest.html
-->
<head>
<title> AcceleratorTest </title>
</head>
<body>
<h1>AcceleratorTest<br>Bug ID: </h1>
<p> See the dialog box (usually in upper left corner) for instructions</p>
<APPLET CODE="AcceleratorTest.class" WIDTH=200 HEIGHT=200></APPLET>
</body>
</html>

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 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
@ -22,307 +22,170 @@
*/
/*
test
@bug 6680988
@summary verify that various shortcuts and accelerators work
@author yuri.nesterenko : area=awt.keyboard
@run applet/manual=yesno AcceleratorTest.html
*/
/**
* AcceleratorTest.java
*
* summary:
* @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.applet.Applet;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
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 extends JApplet
{
//Declare things used in the test, like buttons and labels here
static int pressed = 0;
Hashtable<String, Integer> cmdHash = new Hashtable<String, Integer>();
String[] CMD = {
"\u042E, keep me in focus",
"Item Cyrl Be",
"Item English Period",
"Item English N",
"\u0436"
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"
};
JFrame jfr;
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\"";
public void init()
{
//Create instructions for the user here, as well as set up
// the environment -- set the layout manager, add buttons,
// etc.
this.setLayout (new BorderLayout ());
String[] instructions =
{
" Ensure you have Russian keyboard layout as a currently active.",
"(1) Press Ctrl+\u0411 (a key with \",<\" on it) ",
"(2) Find a . (period) in this layout (perhaps \"/?\" or \"7&\" key).",
"Press Ctrl+.",
"(3) Press Crtl+ regular English . (period) key (on \".>\" )",
"(4) Press Ctrl+ key with English N.",
"(5) Press Alt+\u042E (key with \".>\")",
"(6) Press Alt+\u0436 (key with \";:\")",
"If all expected commands will be fired, look for message",
"\"All tests passed\""
};
Sysout.createDialogWithInstructions( instructions );
for(int i = 0; i < CMD.length; i++) {
for (int i = 0; i < CMD.length; i++) {
cmdHash.put(CMD[i], 0);
}
jfr = new JFrame();
JButton jbu;
jfr.add((jbu = new JButton(CMD[0])));
jbu.setMnemonic(java.awt.event.KeyEvent.getExtendedKeyCodeForChar('\u042E'));
jbu.addActionListener( new ALi(CMD[0]));
PassFailJFrame testFrame = new 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);
JMenuBar menuBar = new JMenuBar();
jfr.setJMenuBar(menuBar);
JMenu menu = new JMenu("Menu");
menuBar.add(menu);
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]));
JMenuItem menuItem = new JMenuItem(CMD[1]);
menuItem.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.getExtendedKeyCodeForChar('\u0431'),
InputEvent.CTRL_DOWN_MASK));
JMenuBar menuBar = new JMenuBar();
jfr.setJMenuBar(menuBar);
JMenu menu = new JMenu("Menu");
menuBar.add(menu);
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);
JMenuItem menuItem = new JMenuItem(CMD[1]);
menuItem.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent
.getExtendedKeyCodeForChar('\u0431'), InputEvent.CTRL_DOWN_MASK));
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);
JMenuItem menuItemEnglish = new JMenuItem(CMD[2]);
menuItemEnglish.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD,
InputEvent.CTRL_DOWN_MASK));
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;
}
Sysout.println("Fired");
if( !notYet ) {
Sysout.println("All tests passed.");
}
}
}
);
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.setBounds(650,0,200,200);
jfr.setVisible(true);
output.setVisible(true);
}//End init()
testFrame.awaitAndCheck();
}
public void start ()
{
//Get things going. Request focus, set size, et cetera
setSize (200,200);
setVisible(true);
validate();
}// start()
public class ALi implements ActionListener {
public static class ALi implements ActionListener {
String expectedCmd;
public ALi( String eCmd ) {
public ALi(String eCmd) {
expectedCmd = eCmd;
}
public void actionPerformed(ActionEvent ae) {
if( cmdHash.containsKey(ae.getActionCommand()) ) {
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;
//Sysout.println(CMD[i]+":"+cmdHash.get(CMD[i]));
for (int i = 0; i < CMD.length; i++) {
if (cmdHash.get(CMD[i]) == 0) notYet = true;
//text.append(CMD[i]+":"+cmdHash.get(CMD[i]));
}
Sysout.println("FIRED");
if( !notYet ) {
Sysout.println("All tests passed.");
text.append(ae.getActionCommand() + " FIRED\n");
if (!notYet) {
text.append("All tests passed.\n");
}
}
}
}// class AcceleratorTest
/* Place other classes related to the test after this line */
/****************************************************
Standard Test Machinery
DO NOT modify anything below -- it's a standard
chunk of code whose purpose is to make user
interaction uniform, and thereby make it simpler
to read and understand someone else's test.
****************************************************/
/**
This is part of the standard test machinery.
It creates a dialog (with the instructions), and is the interface
for sending text messages to the user.
To print the instructions, send an array of strings to Sysout.createDialog
WithInstructions method. Put one line of instructions per array entry.
To display a message for the tester to see, simply call Sysout.println
with the string to be displayed.
This mimics System.out.println but works within the test harness as well
as standalone.
*/
class Sysout
{
private static TestDialog dialog;
private static boolean numbering = false;
private static int messageNumber = 0;
public static void createDialogWithInstructions( String[] instructions )
{
dialog = new TestDialog( new Frame(), "Instructions" );
dialog.printInstructions( instructions );
dialog.setVisible(true);
println( "Any messages for the tester will display here." );
}
public static void createDialog( )
{
dialog = new TestDialog( new Frame(), "Instructions" );
String[] defInstr = { "Instructions will appear here. ", "" } ;
dialog.printInstructions( defInstr );
dialog.setVisible(true);
println( "Any messages for the tester will display here." );
}
/* Enables message counting for the tester. */
public static void enableNumbering(boolean enable){
numbering = enable;
}
public static void printInstructions( String[] instructions )
{
dialog.printInstructions( instructions );
}
public static void println( String messageIn )
{
if (numbering) {
messageIn = "" + messageNumber + " " + messageIn;
messageNumber++;
}
dialog.displayMessage( messageIn );
}
}// Sysout class
/**
This is part of the standard test machinery. It provides a place for the
test instructions to be displayed, and a place for interactive messages
to the user to be displayed.
To have the test instructions displayed, see Sysout.
To have a message to the user be displayed, see Sysout.
Do not call anything in this dialog directly.
*/
class TestDialog extends Dialog
{
TextArea instructionsText;
TextArea messageText;
int maxStringLength = 80;
//DO NOT call this directly, go through Sysout
public TestDialog( Frame frame, String name )
{
super( frame, name );
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
add( "North", instructionsText );
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
add("Center", messageText);
pack();
setVisible(true);
}// TestDialog()
//DO NOT call this directly, go through Sysout
public void printInstructions( String[] instructions )
{
//Clear out any current instructions
instructionsText.setText( "" );
//Go down array of instruction strings
String printStr, remainingStr;
for( int i=0; i < instructions.length; i++ )
{
//chop up each into pieces maxSringLength long
remainingStr = instructions[ i ];
while( remainingStr.length() > 0 )
{
//if longer than max then chop off first max chars to print
if( remainingStr.length() >= maxStringLength )
{
//Try to chop on a word boundary
int posOfSpace = remainingStr.
lastIndexOf( ' ', maxStringLength - 1 );
if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
printStr = remainingStr.substring( 0, posOfSpace + 1 );
remainingStr = remainingStr.substring( posOfSpace + 1 );
}
//else just print
else
{
printStr = remainingStr;
remainingStr = "";
}
instructionsText.append( printStr + "\n" );
}// while
}// for
}//printInstructions()
//DO NOT call this directly, go through Sysout
public void displayMessage( String messageIn )
{
messageText.append( messageIn + "\n" );
System.out.println(messageIn);
}
}// TestDialog class
}