Skip to content

Commit f4772b5

Browse files
author
Vladimir Kempik
committed
8272602: [macos] not all KEY_PRESSED events sent when control modifier is used
Backport-of: ddcd851c43aa97477c7e406490c0c7c7d71ac629
1 parent 90d5293 commit f4772b5

File tree

2 files changed

+109
-2
lines changed

2 files changed

+109
-2
lines changed

src/java.desktop/macosx/native/libosxapp/JNIUtilities.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,18 @@
4040
}
4141

4242
jstring NSStringToJavaString(JNIEnv* env, NSString *str) {
43-
4443
if (str == NULL) {
4544
return NULL;
4645
}
47-
jstring jStr = (*env)->NewStringUTF(env, [str UTF8String]);
46+
jsize len = [str length];
47+
unichar *buffer = (unichar*)calloc(len, sizeof(unichar));
48+
if (buffer == NULL) {
49+
return NULL;
50+
}
51+
NSRange crange = NSMakeRange(0, len);
52+
[str getCharacters:buffer range:crange];
53+
jstring jStr = (*env)->NewString(env, buffer, len);
54+
free(buffer);
4855
CHECK_EXCEPTION();
4956
return jStr;
5057
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (c) 2021, 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+
@key headful
27+
@bug 8272602
28+
@summary Ctrl+Space should generate a KeyTyped event on macOS
29+
@run main CtrlSpace
30+
*/
31+
32+
import java.awt.BorderLayout;
33+
import java.awt.Frame;
34+
import java.awt.Panel;
35+
import java.awt.TextField;
36+
import java.awt.Robot;
37+
import java.awt.event.KeyEvent;
38+
import java.awt.event.KeyListener;
39+
40+
public class CtrlSpace extends Frame implements KeyListener {
41+
42+
static volatile boolean testPassed = false;
43+
static volatile Robot robot;
44+
45+
public static void main(String[] args) throws Exception {
46+
47+
robot = new Robot();
48+
robot.setAutoWaitForIdle(true);
49+
robot.setAutoDelay(100);
50+
51+
Frame frame = createAndShowGUI(robot);
52+
53+
test(robot);
54+
robot.waitForIdle();
55+
Thread.sleep(2000);
56+
frame.setVisible(false);
57+
frame.dispose();
58+
if (!testPassed) {
59+
throw new RuntimeException("No KeyTyped event");
60+
}
61+
}
62+
63+
64+
static Frame createAndShowGUI(Robot robot) {
65+
CtrlSpace win = new CtrlSpace();
66+
win.setSize(300, 300);
67+
Panel panel = new Panel(new BorderLayout());
68+
TextField textField = new TextField("abcdefghijk");
69+
textField.addKeyListener(win);
70+
panel.add(textField, BorderLayout.CENTER);
71+
win.add(panel);
72+
win.setVisible(true);
73+
robot.waitForIdle();
74+
textField.requestFocusInWindow();
75+
robot.waitForIdle();
76+
return win;
77+
}
78+
79+
public static void test(Robot robot) {
80+
robot.keyPress(KeyEvent.VK_CONTROL);
81+
robot.keyPress(KeyEvent.VK_SPACE);
82+
robot.keyRelease(KeyEvent.VK_SPACE);
83+
robot.keyRelease(KeyEvent.VK_CONTROL);
84+
robot.delay(200);
85+
}
86+
87+
public void keyPressed(KeyEvent evt) {
88+
System.out.println("Pressed " + evt);
89+
}
90+
91+
public void keyReleased(KeyEvent evt) {
92+
System.out.println("Released " + evt);
93+
}
94+
95+
public void keyTyped(KeyEvent evt) {
96+
System.out.println("Typed " + evt);
97+
testPassed = true;
98+
}
99+
100+
}

0 commit comments

Comments
 (0)