1 /*
   2  * Copyright (c) 2011, 2015, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #import "java_awt_event_InputEvent.h"
  27 #import "java_awt_event_KeyEvent.h"
  28 #import "LWCToolkit.h"
  29 
  30 #import "jni_util.h"
  31 
  32 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  33 #import <sys/time.h>
  34 #import <Carbon/Carbon.h>
  35 
  36 /*
  37  * Table to map typed characters to their Java virtual key equivalent and back.
  38  * We use the incoming unichar (ignoring all modifiers) and try to figure out
  39  * which virtual key code is appropriate. A lot of them just have direct
  40  * mappings (the function keys, arrow keys, etc.) so they aren't a problem.
  41  * We had to do something a little funky to catch the keys on the numeric
  42  * key pad (i.e. using event mask to distinguish between period on regular
  43  * keyboard and decimal on keypad). We also have to do something incredibly
  44  * hokey with regards to the shifted punctuation characters. For examples,
  45  * consider '&' which is usually Shift-7.  For the Java key typed events,
  46  * that's no problem, we just say pass the unichar. But for the
  47  * KeyPressed/Released events, we need to identify the virtual key code
  48  * (which roughly correspond to hardware keys) which means we are supposed
  49  * to say the virtual 7 key was pressed.  But how are we supposed to know
  50  * when we get a punctuation char what was the real hardware key was that
  51  * was pressed?  Although '&' often comes from Shift-7 the keyboard can be
  52  * remapped!  I don't think there really is a good answer, and hopefully
  53  * all good applets are only interested in logical key typed events not
  54  * press/release.  Meanwhile, we are hard-coding the shifted punctuation
  55  * to trigger the virtual keys that are the expected ones under a standard
  56  * keymapping. Looking at Windows & Mac, they don't actually do this, the
  57  * Mac seems to just put the ascii code in for the shifted punctuation
  58  * (which means they actually end up with bogus key codes on the Java side),
  59  * Windows I can't even figure out what it's doing.
  60  */
  61 #define KL_STANDARD java_awt_event_KeyEvent_KEY_LOCATION_STANDARD
  62 #define KL_NUMPAD   java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD
  63 #define KL_UNKNOWN  java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN
  64 static struct _key
  65 {
  66     unsigned short keyCode;
  67     BOOL postsTyped;
  68     jint javaKeyLocation;
  69     jint javaKeyCode;
  70 }
  71 const keyTable[] =
  72 {
  73     {0x00, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_A},
  74     {0x01, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_S},
  75     {0x02, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_D},
  76     {0x03, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_F},
  77     {0x04, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_H},
  78     {0x05, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_G},
  79     {0x06, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_Z},
  80     {0x07, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_X},
  81     {0x08, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_C},
  82     {0x09, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_V},
  83     {0x0A, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_BACK_QUOTE},
  84     {0x0B, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_B},
  85     {0x0C, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_Q},
  86     {0x0D, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_W},
  87     {0x0E, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_E},
  88     {0x0F, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_R},
  89     {0x10, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_Y},
  90     {0x11, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_T},
  91     {0x12, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_1},
  92     {0x13, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_2},
  93     {0x14, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_3},
  94     {0x15, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_4},
  95     {0x16, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_6},
  96     {0x17, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_5},
  97     {0x18, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_EQUALS},
  98     {0x19, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_9},
  99     {0x1A, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_7},
 100     {0x1B, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_MINUS},
 101     {0x1C, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_8},
 102     {0x1D, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_0},
 103     {0x1E, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_CLOSE_BRACKET},
 104     {0x1F, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_O},
 105     {0x20, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_U},
 106     {0x21, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_OPEN_BRACKET},
 107     {0x22, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_I},
 108     {0x23, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_P},
 109     {0x24, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_ENTER},
 110     {0x25, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_L},
 111     {0x26, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_J},
 112     {0x27, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_QUOTE},
 113     {0x28, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_K},
 114     {0x29, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_SEMICOLON},
 115     {0x2A, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_BACK_SLASH},
 116     {0x2B, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_COMMA},
 117     {0x2C, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_SLASH},
 118     {0x2D, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_N},
 119     {0x2E, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_M},
 120     {0x2F, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_PERIOD},
 121     {0x30, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_TAB},
 122     {0x31, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_SPACE},
 123     {0x32, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_BACK_QUOTE},
 124     {0x33, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_BACK_SPACE},
 125     {0x34, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_ENTER},
 126     {0x35, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_ESCAPE},
 127     {0x36, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 128     {0x37, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_META},      // ****
 129     {0x38, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_SHIFT},     // ****
 130     {0x39, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_CAPS_LOCK},
 131     {0x3A, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_ALT},       // ****
 132     {0x3B, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_CONTROL},   // ****
 133     {0x3C, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 134     {0x3D, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 135     {0x3E, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 136     {0x3F, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED}, // the 'fn' key on PowerBooks
 137     {0x40, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 138     {0x41, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_DECIMAL},
 139     {0x42, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 140     {0x43, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_MULTIPLY},
 141     {0x44, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 142     {0x45, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_ADD},
 143     {0x46, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 144     {0x47, NO,  KL_NUMPAD,   java_awt_event_KeyEvent_VK_CLEAR},
 145     {0x48, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 146     {0x49, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 147     {0x4A, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 148     {0x4B, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_DIVIDE},
 149     {0x4C, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_ENTER},
 150     {0x4D, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 151     {0x4E, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_SUBTRACT},
 152     {0x4F, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 153     {0x50, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 154     {0x51, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_EQUALS},
 155     {0x52, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD0},
 156     {0x53, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD1},
 157     {0x54, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD2},
 158     {0x55, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD3},
 159     {0x56, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD4},
 160     {0x57, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD5},
 161     {0x58, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD6},
 162     {0x59, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD7},
 163     {0x5A, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 164     {0x5B, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD8},
 165     {0x5C, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_NUMPAD9},
 166     {0x5D, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_BACK_SLASH}, // This is a combo yen/backslash on JIS keyboards.
 167     {0x5E, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_UNDERSCORE},
 168     {0x5F, YES, KL_NUMPAD,   java_awt_event_KeyEvent_VK_COMMA},
 169     {0x60, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F5},
 170     {0x61, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F6},
 171     {0x62, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F7},
 172     {0x63, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F3},
 173     {0x64, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F8},
 174     {0x65, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F9},
 175     {0x66, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_ALPHANUMERIC},
 176     {0x67, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F11},
 177     {0x68, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_KATAKANA},
 178     {0x69, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F13},
 179     {0x6A, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F16},
 180     {0x6B, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F14},
 181     {0x6C, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 182     {0x6D, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F10},
 183     {0x6E, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 184     {0x6F, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F12},
 185     {0x70, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 186     {0x71, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F15},
 187     {0x72, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_HELP},
 188     {0x73, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_HOME},
 189     {0x74, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_PAGE_UP},
 190     {0x75, YES, KL_STANDARD, java_awt_event_KeyEvent_VK_DELETE},
 191     {0x76, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F4},
 192     {0x77, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_END},
 193     {0x78, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F2},
 194     {0x79, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_PAGE_DOWN},
 195     {0x7A, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_F1},
 196     {0x7B, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_LEFT},
 197     {0x7C, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_RIGHT},
 198     {0x7D, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_DOWN},
 199     {0x7E, NO,  KL_STANDARD, java_awt_event_KeyEvent_VK_UP},
 200     {0x7F, NO,  KL_UNKNOWN,  java_awt_event_KeyEvent_VK_UNDEFINED},
 201 };
 202 
 203 /*
 204  * This table was stolen from the Windows implementation for mapping
 205  * Unicode values to VK codes for dead keys.  On Windows, some layouts
 206  * return ASCII punctuation for dead accents, while some return spacing
 207  * accent chars, so both should be listed.  However, in all of the
 208  * keyboard layouts I tried only the Unicode values are used.
 209  */
 210 struct CharToVKEntry {
 211     UniChar c;
 212     jint javaKey;
 213 };
 214 static const struct CharToVKEntry charToDeadVKTable[] = {
 215     {0x0060, java_awt_event_KeyEvent_VK_DEAD_GRAVE},
 216     {0x00B4, java_awt_event_KeyEvent_VK_DEAD_ACUTE},
 217     {0x0384, java_awt_event_KeyEvent_VK_DEAD_ACUTE}, // Unicode "GREEK TONOS" -- Greek keyboard, semicolon key
 218     {0x005E, java_awt_event_KeyEvent_VK_DEAD_CIRCUMFLEX},
 219     {0x007E, java_awt_event_KeyEvent_VK_DEAD_TILDE},
 220     {0x02DC, java_awt_event_KeyEvent_VK_DEAD_TILDE}, // Unicode "SMALL TILDE"
 221     {0x00AF, java_awt_event_KeyEvent_VK_DEAD_MACRON},
 222     {0x02D8, java_awt_event_KeyEvent_VK_DEAD_BREVE},
 223     {0x02D9, java_awt_event_KeyEvent_VK_DEAD_ABOVEDOT},
 224     {0x00A8, java_awt_event_KeyEvent_VK_DEAD_DIAERESIS},
 225     {0x02DA, java_awt_event_KeyEvent_VK_DEAD_ABOVERING},
 226     {0x02DD, java_awt_event_KeyEvent_VK_DEAD_DOUBLEACUTE},
 227     {0x02C7, java_awt_event_KeyEvent_VK_DEAD_CARON},
 228     {0x00B8, java_awt_event_KeyEvent_VK_DEAD_CEDILLA},
 229     {0x02DB, java_awt_event_KeyEvent_VK_DEAD_OGONEK},
 230     {0x037A, java_awt_event_KeyEvent_VK_DEAD_IOTA},
 231     {0x309B, java_awt_event_KeyEvent_VK_DEAD_VOICED_SOUND},
 232     {0x309C, java_awt_event_KeyEvent_VK_DEAD_SEMIVOICED_SOUND},
 233     {0,0}
 234 };
 235 
 236 // TODO: some constants below are part of CGS (private interfaces)...
 237 // for now we will look at the raw key code to determine left/right status
 238 // but not sure this is foolproof...
 239 static struct _nsKeyToJavaModifier
 240 {
 241     NSUInteger nsMask;
 242     //NSUInteger cgsLeftMask;
 243     //NSUInteger cgsRightMask;
 244     unsigned short leftKeyCode;
 245     unsigned short rightKeyCode;
 246     jint javaExtMask;
 247     jint javaMask;
 248     jint javaKey;
 249 }
 250 const nsKeyToJavaModifierTable[] =
 251 {
 252     {
 253         NSAlphaShiftKeyMask,
 254         0,
 255         0,
 256         0, // no Java equivalent
 257         0, // no Java equivalent
 258         java_awt_event_KeyEvent_VK_CAPS_LOCK
 259     },
 260     {
 261         NSShiftKeyMask,
 262         //kCGSFlagsMaskAppleShiftKey,
 263         //kCGSFlagsMaskAppleRightShiftKey,
 264         56,
 265         60,
 266         java_awt_event_InputEvent_SHIFT_DOWN_MASK,
 267         java_awt_event_InputEvent_SHIFT_MASK,
 268         java_awt_event_KeyEvent_VK_SHIFT
 269     },
 270     {
 271         NSControlKeyMask,
 272         //kCGSFlagsMaskAppleControlKey,
 273         //kCGSFlagsMaskAppleRightControlKey,
 274         59,
 275         62,
 276         java_awt_event_InputEvent_CTRL_DOWN_MASK,
 277         java_awt_event_InputEvent_CTRL_MASK,
 278         java_awt_event_KeyEvent_VK_CONTROL
 279     },
 280     {
 281         NSAlternateKeyMask,
 282         //kCGSFlagsMaskAppleLeftAlternateKey,
 283         //kCGSFlagsMaskAppleRightAlternateKey,
 284         58,
 285         61,
 286         java_awt_event_InputEvent_ALT_DOWN_MASK,
 287         java_awt_event_InputEvent_ALT_MASK,
 288         java_awt_event_KeyEvent_VK_ALT
 289     },
 290     {
 291         NSCommandKeyMask,
 292         //kCGSFlagsMaskAppleLeftCommandKey,
 293         //kCGSFlagsMaskAppleRightCommandKey,
 294         55,
 295         54,
 296         java_awt_event_InputEvent_META_DOWN_MASK,
 297         java_awt_event_InputEvent_META_MASK,
 298         java_awt_event_KeyEvent_VK_META
 299     },
 300     // NSNumericPadKeyMask
 301     {
 302         NSHelpKeyMask,
 303         0,
 304         0,
 305         0, // no Java equivalent
 306         0, // no Java equivalent
 307         java_awt_event_KeyEvent_VK_HELP
 308     },
 309     // NSFunctionKeyMask
 310     {0, 0, 0, 0, 0, 0}
 311 };
 312 
 313 /*
 314  * Almost all unicode characters just go from NS to Java with no translation.
 315  *  For the few exceptions, we handle it here with this small table.
 316  */
 317 #define ALL_NS_KEY_MODIFIERS_MASK \
 318     (NSShiftKeyMask | NSControlKeyMask | NSAlternateKeyMask | NSCommandKeyMask)
 319 
 320 static struct _char {
 321     NSUInteger modifier;
 322     unichar nsChar;
 323     unichar javaChar;
 324 }
 325 const charTable[] = {
 326     // map enter on keypad to same as return key
 327     {0,                         NSEnterCharacter,          NSNewlineCharacter},
 328 
 329     // [3134616] return newline instead of carriage return
 330     {0,                         NSCarriageReturnCharacter, NSNewlineCharacter},
 331 
 332     // "delete" means backspace in Java
 333     {ALL_NS_KEY_MODIFIERS_MASK, NSDeleteCharacter,         NSBackspaceCharacter},
 334     {ALL_NS_KEY_MODIFIERS_MASK, NSDeleteFunctionKey,       NSDeleteCharacter},
 335 
 336     // back-tab is only differentiated from tab by Shift flag
 337     {NSShiftKeyMask,            NSBackTabCharacter,        NSTabCharacter},
 338 
 339     {0, 0, 0}
 340 };
 341 
 342 unichar NsCharToJavaChar(unichar nsChar, NSUInteger modifiers)
 343 {
 344     const struct _char *cur;
 345     // Mask off just the keyboard modifiers from the event modifier mask.
 346     NSUInteger testableFlags = (modifiers & ALL_NS_KEY_MODIFIERS_MASK);
 347 
 348     // walk through table & find the match
 349     for (cur = charTable; cur->nsChar != 0 ; cur++) {
 350         // <rdar://Problem/3476426> Need to determine if we are looking at
 351         // a plain keypress or a modified keypress.  Don't adjust the
 352         // character of a keypress with a modifier.
 353         if (cur->nsChar == nsChar) {
 354             if (cur->modifier == 0 && testableFlags == 0) {
 355                 // If the modifier field is 0, that means to transform
 356                 // this character if no additional keyboard modifiers are set.
 357                 // This lets ctrl-C be reported as ctrl-C and not transformed
 358                 // into Newline.
 359                 return cur->javaChar;
 360             } else if (cur->modifier != 0 &&
 361                        (testableFlags & cur->modifier) == testableFlags)
 362             {
 363                 // Likewise, if the modifier field is nonzero, that means
 364                 // transform this character if only these modifiers are
 365                 // set in the testable flags.
 366                 return cur->javaChar;
 367             }
 368         }
 369     }
 370 
 371     if (nsChar >= NSUpArrowFunctionKey && nsChar <= NSModeSwitchFunctionKey) {
 372         return java_awt_event_KeyEvent_CHAR_UNDEFINED;
 373     }
 374 
 375     // otherwise return character unchanged
 376     return nsChar;
 377 }
 378 
 379 static unichar NsGetDeadKeyChar(unsigned short keyCode)
 380 {
 381     TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
 382     CFDataRef uchr = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData);
 383     if (uchr == nil) { return 0; }
 384     const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout*)CFDataGetBytePtr(uchr);
 385     // Carbon modifiers should be used instead of NSEvent modifiers
 386     UInt32 modifierKeyState = (GetCurrentEventKeyModifiers() >> 8) & 0xFF;
 387 
 388     if (keyboardLayout) {
 389         UInt32 deadKeyState = 0;
 390         UniCharCount maxStringLength = 255;
 391         UniCharCount actualStringLength = 0;
 392         UniChar unicodeString[maxStringLength];
 393 
 394         // get the deadKeyState
 395         OSStatus status = UCKeyTranslate(keyboardLayout,
 396                                          keyCode, kUCKeyActionDown, modifierKeyState,
 397                                          LMGetKbdType(), kUCKeyTranslateNoDeadKeysBit,
 398                                          &deadKeyState,
 399                                          maxStringLength,
 400                                          &actualStringLength, unicodeString);
 401 
 402         if (status == noErr && deadKeyState != 0) {
 403             // Press SPACE to get the dead key char
 404             status = UCKeyTranslate(keyboardLayout,
 405                                     kVK_Space, kUCKeyActionDown, 0,
 406                                     LMGetKbdType(), 0,
 407                                     &deadKeyState,
 408                                     maxStringLength,
 409                                     &actualStringLength, unicodeString);
 410 
 411             if (status == noErr && actualStringLength > 0) {
 412                 return unicodeString[0];
 413             }
 414         }
 415     }
 416     return 0;
 417 }
 418 
 419 /*
 420  * This is the function that uses the table above to take incoming
 421  * NSEvent keyCodes and translate to the Java virtual key code.
 422  */
 423 static void
 424 NsCharToJavaVirtualKeyCode(unichar ch, BOOL isDeadChar,
 425                            NSUInteger flags, unsigned short key,
 426                            jint *keyCode, jint *keyLocation, BOOL *postsTyped, unichar *deadChar)
 427 {
 428     static size_t size = sizeof(keyTable) / sizeof(struct _key);
 429     NSInteger offset;
 430 
 431     if (isDeadChar) {
 432         unichar testDeadChar = NsGetDeadKeyChar(key);
 433         const struct CharToVKEntry *map;
 434         for (map = charToDeadVKTable; map->c != 0; ++map) {
 435             if (testDeadChar == map->c) {
 436                 *keyCode = map->javaKey;
 437                 *postsTyped = NO;
 438                 // TODO: use UNKNOWN here?
 439                 *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 440                 *deadChar = testDeadChar;
 441                 return;
 442             }
 443         }
 444         // If we got here, we keep looking for a normal key.
 445     }
 446 
 447     if ([[NSCharacterSet letterCharacterSet] characterIsMember:ch]) {
 448         // key is an alphabetic character
 449         unichar lower;
 450         lower = tolower(ch);
 451         offset = lower - 'a';
 452         if (offset >= 0 && offset <= 25) {
 453             // some chars in letter set are NOT actually A-Z characters?!
 454             // skip them...
 455             *postsTyped = YES;
 456             // do quick conversion
 457             *keyCode = java_awt_event_KeyEvent_VK_A + offset;
 458             *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_STANDARD;
 459             return;
 460         }
 461     }
 462 
 463     if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:ch]) {
 464         // key is a digit
 465         offset = ch - '0';
 466         // make sure in range for decimal digits
 467         if (offset >= 0 && offset <= 9)    {
 468             jboolean numpad = (flags & NSNumericPadKeyMask) != 0;
 469             *postsTyped = YES;
 470             if (numpad) {
 471                 *keyCode = offset + java_awt_event_KeyEvent_VK_NUMPAD0;
 472                 *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD;
 473             } else {
 474                 *keyCode = offset + java_awt_event_KeyEvent_VK_0;
 475                 *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_STANDARD;
 476             }
 477             return;
 478         }
 479     }
 480 
 481     if (key < size) {
 482         *postsTyped = keyTable[key].postsTyped;
 483         *keyCode = keyTable[key].javaKeyCode;
 484         *keyLocation = keyTable[key].javaKeyLocation;
 485     } else {
 486         // Should we report this? This means we've got a keyboard
 487         // we don't know about...
 488         *postsTyped = NO;
 489         *keyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
 490         *keyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 491     }
 492 }
 493 
 494 /*
 495  * This returns the java key data for the key NSEvent modifiers
 496  * (after NSFlagChanged).
 497  */
 498 static void
 499 NsKeyModifiersToJavaKeyInfo(NSUInteger nsFlags, unsigned short eventKeyCode,
 500                             jint *javaKeyCode,
 501                             jint *javaKeyLocation,
 502                             jint *javaKeyType)
 503 {
 504     static NSUInteger sPreviousNSFlags = 0;
 505 
 506     const struct _nsKeyToJavaModifier* cur;
 507     NSUInteger oldNSFlags = sPreviousNSFlags;
 508     NSUInteger changedNSFlags = oldNSFlags ^ nsFlags;
 509     sPreviousNSFlags = nsFlags;
 510 
 511     *javaKeyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
 512     *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 513     *javaKeyType = java_awt_event_KeyEvent_KEY_PRESSED;
 514 
 515     for (cur = nsKeyToJavaModifierTable; cur->nsMask != 0; ++cur) {
 516         if (changedNSFlags & cur->nsMask) {
 517             *javaKeyCode = cur->javaKey;
 518             *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_STANDARD;
 519             // TODO: uses SPI...
 520             //if (changedNSFlags & cur->cgsLeftMask) {
 521             //    *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_LEFT;
 522             //} else if (changedNSFlags & cur->cgsRightMask) {
 523             //    *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_RIGHT;
 524             //}
 525             if (eventKeyCode == cur->leftKeyCode) {
 526                 *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_LEFT;
 527             } else if (eventKeyCode == cur->rightKeyCode) {
 528                 *javaKeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_RIGHT;
 529             }
 530             *javaKeyType = (cur->nsMask & nsFlags) ?
 531                 java_awt_event_KeyEvent_KEY_PRESSED :
 532                 java_awt_event_KeyEvent_KEY_RELEASED;
 533             break;
 534         }
 535     }
 536 }
 537 
 538 /*
 539  * This returns the java modifiers for a key NSEvent.
 540  */
 541 jint NsKeyModifiersToJavaModifiers(NSUInteger nsFlags, BOOL isExtMods)
 542 {
 543     jint javaModifiers = 0;
 544     const struct _nsKeyToJavaModifier* cur;
 545 
 546     for (cur = nsKeyToJavaModifierTable; cur->nsMask != 0; ++cur) {
 547         if ((cur->nsMask & nsFlags) != 0) {
 548             javaModifiers |= isExtMods? cur->javaExtMask : cur->javaMask;
 549         }
 550     }
 551 
 552     return javaModifiers;
 553 }
 554 
 555 /*
 556  * This returns the NSEvent flags for java key modifiers.
 557  */
 558 NSUInteger JavaModifiersToNsKeyModifiers(jint javaModifiers, BOOL isExtMods)
 559 {
 560     NSUInteger nsFlags = 0;
 561     const struct _nsKeyToJavaModifier* cur;
 562 
 563     for (cur = nsKeyToJavaModifierTable; cur->nsMask != 0; ++cur) {
 564         jint mask = isExtMods? cur->javaExtMask : cur->javaMask;
 565         if ((mask & javaModifiers) != 0) {
 566             nsFlags |= cur->nsMask;
 567         }
 568     }
 569 
 570     // special case
 571     jint mask = isExtMods? java_awt_event_InputEvent_ALT_GRAPH_DOWN_MASK :
 572                            java_awt_event_InputEvent_ALT_GRAPH_MASK;
 573 
 574     if ((mask & javaModifiers) != 0) {
 575         nsFlags |= NSAlternateKeyMask;
 576     }
 577 
 578     return nsFlags;
 579 }
 580 
 581 
 582 jint GetJavaMouseModifiers(NSUInteger modifierFlags)
 583 {
 584     // Mousing needs the key modifiers
 585     jint modifiers = NsKeyModifiersToJavaModifiers(modifierFlags, YES);
 586 
 587 
 588     /*
 589      * Ask Quartz about mouse buttons state
 590      */
 591 
 592     if (CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState,
 593                                  kCGMouseButtonLeft)) {
 594         modifiers |= java_awt_event_InputEvent_BUTTON1_DOWN_MASK;
 595     }
 596 
 597     if (CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState,
 598                                  kCGMouseButtonRight)) {
 599         modifiers |= java_awt_event_InputEvent_BUTTON3_DOWN_MASK;
 600     }
 601 
 602     if (CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState,
 603                                  kCGMouseButtonCenter)) {
 604         modifiers |= java_awt_event_InputEvent_BUTTON2_DOWN_MASK;
 605     }
 606 
 607     NSInteger extraButton = 3;
 608     for (; extraButton < gNumberOfButtons; extraButton++) {
 609         if (CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState,
 610                                  extraButton)) {
 611             modifiers |= gButtonDownMasks[extraButton];
 612         }
 613     }
 614 
 615     return modifiers;
 616 }
 617 
 618 jlong UTC(NSEvent *event) {
 619     struct timeval tv;
 620     if (gettimeofday(&tv, NULL) == 0) {
 621         long long sec = (long long)tv.tv_sec;
 622         return (sec*1000) + (tv.tv_usec/1000);
 623     }
 624     return 0;
 625 }
 626 
 627 JNIEXPORT void JNICALL
 628 Java_java_awt_AWTEvent_nativeSetSource
 629     (JNIEnv *env, jobject self, jobject newSource)
 630 {
 631 }
 632 
 633 /*
 634  * Class:     sun_lwawt_macosx_NSEvent
 635  * Method:    nsToJavaModifiers
 636  * Signature: (II)I
 637  */
 638 JNIEXPORT jint JNICALL
 639 Java_sun_lwawt_macosx_NSEvent_nsToJavaModifiers
 640 (JNIEnv *env, jclass cls, jint modifierFlags)
 641 {
 642     jint jmodifiers = 0;
 643 
 644 JNF_COCOA_ENTER(env);
 645 
 646     jmodifiers = GetJavaMouseModifiers(modifierFlags);
 647 
 648 JNF_COCOA_EXIT(env);
 649 
 650     return jmodifiers;
 651 }
 652 
 653 /*
 654  * Class:     sun_lwawt_macosx_NSEvent
 655  * Method:    nsToJavaKeyInfo
 656  * Signature: ([I[I)Z
 657  */
 658 JNIEXPORT jboolean JNICALL
 659 Java_sun_lwawt_macosx_NSEvent_nsToJavaKeyInfo
 660 (JNIEnv *env, jclass cls, jintArray inData, jintArray outData)
 661 {
 662     BOOL postsTyped = NO;
 663 
 664 JNF_COCOA_ENTER(env);
 665 
 666     jboolean copy = JNI_FALSE;
 667     jint *data = (*env)->GetIntArrayElements(env, inData, &copy);
 668     CHECK_NULL_RETURN(data, postsTyped);
 669 
 670     // in  = [testChar, testDeadChar, modifierFlags, keyCode]
 671     jchar testChar = (jchar)data[0];
 672     BOOL isDeadChar = (data[1] != 0);
 673     jint modifierFlags = data[2];
 674     jshort keyCode = (jshort)data[3];
 675 
 676     jint jkeyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
 677     jint jkeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 678     jchar testDeadChar = 0;
 679 
 680     NsCharToJavaVirtualKeyCode((unichar)testChar, isDeadChar,
 681                                (NSUInteger)modifierFlags, (unsigned short)keyCode,
 682                                &jkeyCode, &jkeyLocation, &postsTyped, &testDeadChar);
 683 
 684     // out = [jkeyCode, jkeyLocation];
 685     (*env)->SetIntArrayRegion(env, outData, 0, 1, &jkeyCode);
 686     (*env)->SetIntArrayRegion(env, outData, 1, 1, &jkeyLocation);
 687     (*env)->SetIntArrayRegion(env, outData, 2, 1, (jint *)&testDeadChar);
 688 
 689     (*env)->ReleaseIntArrayElements(env, inData, data, 0);
 690 
 691 JNF_COCOA_EXIT(env);
 692 
 693     return postsTyped;
 694 }
 695 
 696 /*
 697  * Class:     sun_lwawt_macosx_NSEvent
 698  * Method:    nsKeyModifiersToJavaKeyInfo
 699  * Signature: ([I[I)V
 700  */
 701 JNIEXPORT void JNICALL
 702 Java_sun_lwawt_macosx_NSEvent_nsKeyModifiersToJavaKeyInfo
 703 (JNIEnv *env, jclass cls, jintArray inData, jintArray outData)
 704 {
 705 JNF_COCOA_ENTER(env);
 706 
 707     jboolean copy = JNI_FALSE;
 708     jint *data = (*env)->GetIntArrayElements(env, inData, &copy);
 709     CHECK_NULL(data);
 710 
 711     // in  = [modifierFlags, keyCode]
 712     jint modifierFlags = data[0];
 713     jshort keyCode = (jshort)data[1];
 714 
 715     jint jkeyCode = java_awt_event_KeyEvent_VK_UNDEFINED;
 716     jint jkeyLocation = java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN;
 717     jint jkeyType = java_awt_event_KeyEvent_KEY_PRESSED;
 718 
 719     NsKeyModifiersToJavaKeyInfo(modifierFlags,
 720                                 keyCode,
 721                                 &jkeyCode,
 722                                 &jkeyLocation,
 723                                 &jkeyType);
 724 
 725     // out = [jkeyCode, jkeyLocation, jkeyType];
 726     (*env)->SetIntArrayRegion(env, outData, 0, 1, &jkeyCode);
 727     (*env)->SetIntArrayRegion(env, outData, 1, 1, &jkeyLocation);
 728     (*env)->SetIntArrayRegion(env, outData, 2, 1, &jkeyType);
 729 
 730     (*env)->ReleaseIntArrayElements(env, inData, data, 0);
 731 
 732 JNF_COCOA_EXIT(env);
 733 }
 734 
 735 /*
 736  * Class:     sun_lwawt_macosx_NSEvent
 737  * Method:    nsToJavaChar
 738  * Signature: (CI)C
 739  */
 740 JNIEXPORT jint JNICALL
 741 Java_sun_lwawt_macosx_NSEvent_nsToJavaChar
 742 (JNIEnv *env, jclass cls, jchar nsChar, jint modifierFlags)
 743 {
 744     jchar javaChar = 0;
 745 
 746 JNF_COCOA_ENTER(env);
 747 
 748     javaChar = NsCharToJavaChar(nsChar, modifierFlags);
 749 
 750 JNF_COCOA_EXIT(env);
 751 
 752     return javaChar;
 753 }