Skip to content

Commit

Permalink
8218917: KeyEvent.getModifiers() returns inconsistent values for ALT …
Browse files Browse the repository at this point in the history
…keys

Backport-of: 2044245
  • Loading branch information
Amos Shi committed Apr 9, 2024
1 parent aa16bfe commit 21f04a2
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 15 deletions.
21 changes: 7 additions & 14 deletions src/java.desktop/macosx/native/libawt_lwawt/awt/AWTEvent.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2019, 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
Expand Down Expand Up @@ -318,7 +318,6 @@
};

static BOOL leftAltKeyPressed;
static BOOL altGRPressed = NO;

/*
* Almost all unicode characters just go from NS to Java with no translation.
Expand Down Expand Up @@ -566,19 +565,13 @@ jint NsKeyModifiersToJavaModifiers(NSUInteger nsFlags, BOOL isExtMods)

for (cur = nsKeyToJavaModifierTable; cur->nsMask != 0; ++cur) {
if ((cur->nsMask & nsFlags) != 0) {

if (cur->nsMask == NSAlternateKeyMask) {
if (leftAltKeyPressed == YES) {
javaModifiers |= isExtMods? cur->javaExtMask : cur->javaMask;
if (altGRPressed == NO)
break;
} else {
leftAltKeyPressed = YES;
altGRPressed = YES;
continue;
}
}
//This code will consider the mask value for left alt as well as
//right alt, but that should be ok, since right alt contains left alt
//mask value.
javaModifiers |= isExtMods ? cur->javaExtMask : cur->javaMask;
if (cur->nsMask == NSAlternateKeyMask && leftAltKeyPressed) {
break; //since right alt key struct is defined last, break out of the loop }
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
@test
@bug 8041928 8158616
@summary Confirm that the Alt-Gr Modifier bit is set correctly.
@requires (os.family != "windows" & os.family != "mac")
@run main/manual AltGraphModifierTest
*/

Expand Down
105 changes: 105 additions & 0 deletions test/jdk/java/awt/keyboard/8218917/AltKeyBug.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2019, 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 8218917
* @summary Tests whether sending an ALT_GRAPH key once, will result in the
* system reporting only ALT_GRAPH even if an ALT was sent and vice versa.
* @run main AltKeyBug
*/
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import java.awt.Robot;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class AltKeyBug {
private static JFrame f;
private static boolean rightAltPressed = false;
private static boolean throwException = false;
private static String errorString;
public static void main(String[] args) throws Exception {
try {
Robot robot = new Robot();
robot.setAutoDelay(50);

SwingUtilities.invokeAndWait(() -> {
JTextField comp = new JTextField();
comp.addKeyListener(new KeyListener() {
@Override public void keyTyped(KeyEvent e) {}
@Override public void keyPressed(KeyEvent e) {
System.out.println("ModEx : " +e.getModifiersEx());
System.out.println("Mod : " +e.getModifiers());
System.out.println("ALT_DOWN : " + e.isAltDown());
System.out.println("ALT_GR_DOWN: " + e.isAltGraphDown());
System.out.println("-----------");
if (rightAltPressed && !e.isAltGraphDown()) {
throwException = true;
errorString = "Right Alt press was sent but not received back.";
} else if (!rightAltPressed && e.isAltGraphDown()) {
throwException = true;
errorString = "Left Alt press was sent, but received Right Alt";
}
}
@Override public void keyReleased(KeyEvent e) {}
});
f = new JFrame();
f.add(comp);
f.setSize(100,100);
f.setVisible(true);
});

for(int i = 0; i < 20; i++) {
rightAltPressed = true;
robot.keyPress(KeyEvent.VK_ALT_GRAPH);
robot.keyRelease(KeyEvent.VK_ALT_GRAPH);

robot.waitForIdle();

if (throwException) {
throw new RuntimeException(errorString);
}
rightAltPressed = false;
robot.keyPress(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_ALT);

robot.waitForIdle();

if (throwException) {
throw new RuntimeException(errorString);
}
}
} finally {
SwingUtilities.invokeAndWait(()-> {
if (f != null)
f.dispose();
});
}

System.out.println("Test passed.");
}
}

1 comment on commit 21f04a2

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.