From aca74a151c93bdde2e60cfe720926818daba2ad1 Mon Sep 17 00:00:00 2001 From: Alexander Scherbatiy Date: Fri, 21 Sep 2012 13:48:06 +0400 Subject: [PATCH] 7199180: [macosx] Dead keys handling for input methods Reviewed-by: kizune, anthony --- .../sun/lwawt/macosx/CPlatformResponder.java | 3 + jdk/src/macosx/native/sun/awt/AWTEvent.m | 7 +- jdk/src/macosx/native/sun/awt/AWTView.m | 5 +- .../DeadKey/DeadKeyMacOSXInputText.java | 131 ++++++++++++++++++ 4 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 jdk/test/java/awt/event/KeyEvent/DeadKey/DeadKeyMacOSXInputText.java diff --git a/jdk/src/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java b/jdk/src/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java index d874b26fb70..a7580a6f0cd 100644 --- a/jdk/src/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java +++ b/jdk/src/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java @@ -160,6 +160,9 @@ final class CPlatformResponder { if(isDeadChar){ testChar = (char) out[2]; + if(testChar == 0){ + return; + } } jkeyCode = out[0]; diff --git a/jdk/src/macosx/native/sun/awt/AWTEvent.m b/jdk/src/macosx/native/sun/awt/AWTEvent.m index 85c9fa9718e..7bde46f1709 100644 --- a/jdk/src/macosx/native/sun/awt/AWTEvent.m +++ b/jdk/src/macosx/native/sun/awt/AWTEvent.m @@ -383,6 +383,7 @@ static unichar NsGetDeadKeyChar(unsigned short keyCode) { TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource(); CFDataRef uchr = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData); + if (uchr == nil) { return; } const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout*)CFDataGetBytePtr(uchr); // Carbon modifiers should be used instead of NSEvent modifiers UInt32 modifierKeyState = (GetCurrentEventKeyModifiers() >> 8) & 0xFF; @@ -563,18 +564,18 @@ NSUInteger JavaModifiersToNsKeyModifiers(jint javaModifiers, BOOL isExtMods) const struct _nsKeyToJavaModifier* cur; for (cur = nsKeyToJavaModifierTable; cur->nsMask != 0; ++cur) { - jint mask = isExtMods? cur->javaExtMask : cur->javaMask; + jint mask = isExtMods? cur->javaExtMask : cur->javaMask; if ((mask & javaModifiers) != 0) { nsFlags |= cur->nsMask; } } // special case - jint mask = isExtMods? java_awt_event_InputEvent_ALT_GRAPH_DOWN_MASK : + jint mask = isExtMods? java_awt_event_InputEvent_ALT_GRAPH_DOWN_MASK : java_awt_event_InputEvent_ALT_GRAPH_MASK; if ((mask & javaModifiers) != 0) { - nsFlags |= NSAlternateKeyMask; + nsFlags |= NSAlternateKeyMask; } return nsFlags; diff --git a/jdk/src/macosx/native/sun/awt/AWTView.m b/jdk/src/macosx/native/sun/awt/AWTView.m index 2d271777321..79219e4c55d 100644 --- a/jdk/src/macosx/native/sun/awt/AWTView.m +++ b/jdk/src/macosx/native/sun/awt/AWTView.m @@ -279,7 +279,10 @@ AWT_ASSERT_APPKIT_THREAD; return; } - if (![self hasMarkedText] && fKeyEventsNeeded) { + NSString *eventCharacters = [event characters]; + BOOL isDeadKey = (eventCharacters != nil && [eventCharacters length] == 0); + + if ((![self hasMarkedText] && fKeyEventsNeeded) || isDeadKey) { [self deliverJavaKeyEventHelper: event]; } diff --git a/jdk/test/java/awt/event/KeyEvent/DeadKey/DeadKeyMacOSXInputText.java b/jdk/test/java/awt/event/KeyEvent/DeadKey/DeadKeyMacOSXInputText.java new file mode 100644 index 00000000000..3ae73c6e684 --- /dev/null +++ b/jdk/test/java/awt/event/KeyEvent/DeadKey/DeadKeyMacOSXInputText.java @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2012, 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 + * @bug 7199180 + * @summary [macosx] Dead keys handling for input methods + * @author alexandr.scherbatiy area=awt.event + * @run main DeadKeyMacOSXInputText + */ +import java.awt.*; +import java.awt.event.*; +import java.awt.event.KeyEvent; +import javax.swing.JTextField; +import sun.awt.OSInfo; +import sun.awt.SunToolkit; + +public class DeadKeyMacOSXInputText { + + private static SunToolkit toolkit; + private static volatile int state = 0; + + public static void main(String[] args) throws Exception { + + if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) { + return; + } + + toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); + Robot robot = new Robot(); + robot.setAutoDelay(50); + + createAndShowGUI(); + + // Pressed keys: Alt + E + A + // Results: ALT + VK_DEAD_ACUTE + a with accute accent + robot.keyPress(KeyEvent.VK_ALT); + robot.keyPress(KeyEvent.VK_E); + robot.keyRelease(KeyEvent.VK_E); + robot.keyRelease(KeyEvent.VK_ALT); + + robot.keyPress(KeyEvent.VK_A); + robot.keyRelease(KeyEvent.VK_A); + toolkit.realSync(); + + if (state != 3) { + throw new RuntimeException("Wrong number of key events."); + } + } + + static void createAndShowGUI() { + Frame frame = new Frame(); + frame.setSize(300, 300); + Panel panel = new Panel(new BorderLayout()); + JTextField textField = new JTextField(); + textField.addKeyListener(new DeadKeyListener()); + panel.add(textField, BorderLayout.CENTER); + frame.add(panel); + frame.setVisible(true); + toolkit.realSync(); + + textField.requestFocusInWindow(); + toolkit.realSync(); + + } + + static class DeadKeyListener extends KeyAdapter { + + @Override + public void keyPressed(KeyEvent e) { + int keyCode = e.getKeyCode(); + char keyChar = e.getKeyChar(); + + switch (state) { + case 0: + if (keyCode != KeyEvent.VK_ALT) { + throw new RuntimeException("Alt is not pressed."); + } + state++; + break; + case 1: + if (keyCode != KeyEvent.VK_DEAD_ACUTE) { + throw new RuntimeException("Dead ACUTE is not pressed."); + } + if (keyChar != 0xB4) { + throw new RuntimeException("Pressed char is not dead acute."); + } + state++; + break; + } + } + + @Override + public void keyTyped(KeyEvent e) { + int keyCode = e.getKeyCode(); + char keyChar = e.getKeyChar(); + + if (state == 2) { + if (keyCode != 0) { + throw new RuntimeException("Key code should be undefined."); + } + if (keyChar != 0xE1) { + throw new RuntimeException("A char does not have ACCUTE accent"); + } + state++; + } else { + throw new RuntimeException("Wron number of keyTyped events."); + } + } + } +}