--- old/src/java.desktop/windows/native/libawt/windows/awt_Robot.cpp 2017-09-08 15:07:18.448704200 +0530 +++ new/src/java.desktop/windows/native/libawt/windows/awt_Robot.cpp 2017-09-08 15:07:17.264992000 +0530 @@ -299,44 +299,81 @@ void AwtRobot::KeyPress( jint jkey ) { - DoKeyEvent(jkey, 0); // no flags means key down + DoKeyEvent(jkey, 0, false); // no flags means key down } void AwtRobot::KeyRelease( jint jkey ) { - DoKeyEvent(jkey, KEYEVENTF_KEYUP); + DoKeyEvent(jkey, KEYEVENTF_KEYUP, false); } -void AwtRobot::DoKeyEvent( jint jkey, DWORD dwFlags ) +void AwtRobot::KeyPressUnicode( jint jkey ) +{ + DoKeyEvent(jkey, 0, true); // no flags means key down +} + +void AwtRobot::KeyReleaseUnicode( jint jkey ) +{ + DoKeyEvent(jkey, KEYEVENTF_KEYUP, true); +} + +void AwtRobot::DoKeyEvent( jint jkey, DWORD dwFlags, BOOL isUnicode ) { UINT vkey; UINT modifiers; UINT scancode; JNIEnv * env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); - // convert Java key into Windows key (and modifiers too) - AwtComponent::JavaKeyToWindowsKey(jkey, &vkey, &modifiers); - if (vkey == 0) { - // no equivalent Windows key found for given Java keycode - JNU_ThrowIllegalArgumentException(env, "Invalid key code"); - } else { - // get the scancode from the virtual key - scancode = ::MapVirtualKey(vkey, 0); - if (vkey == VK_RMENU || - vkey == VK_DELETE || - vkey == VK_INSERT || - vkey == VK_NEXT || - vkey == VK_PRIOR || - vkey == VK_HOME || - vkey == VK_END || - vkey == VK_LEFT || - vkey == VK_RIGHT || - vkey == VK_UP || - vkey == VK_DOWN) { - dwFlags |= KEYEVENTF_EXTENDEDKEY; + if(isUnicode) {printf("In unicode func:%d", jkey); + /* + HandleUnicodeKeys() returns the status of the SendInput() + which returns 0 if there is a fail else non-zero. + The status 0 tells that the SendInput() in unable to interpret + the supplied input upon which the illegal argument exception + would be raised. + */ + if(!HandleUnicodeKeys(jkey, dwFlags)) { + // no equivalent Windows key found for given unicode key + JNU_ThrowIllegalArgumentException(env, "Invalid unicode key"); } - keybd_event(vkey, scancode, dwFlags, 0); - } + } + else { + // convert Java key into Windows key (and modifiers too) + AwtComponent::JavaKeyToWindowsKey(jkey, &vkey, &modifiers); + + if (vkey == 0) { + // no equivalent Windows key found for given Java keycode + JNU_ThrowIllegalArgumentException(env, "Invalid key code"); + } else { + // get the scancode from the virtual key + scancode = ::MapVirtualKey(vkey, 0); + if (vkey == VK_RMENU || + vkey == VK_DELETE || + vkey == VK_INSERT || + vkey == VK_NEXT || + vkey == VK_PRIOR || + vkey == VK_HOME || + vkey == VK_END || + vkey == VK_LEFT || + vkey == VK_RIGHT || + vkey == VK_UP || + vkey == VK_DOWN) { + dwFlags |= KEYEVENTF_EXTENDEDKEY; + } + keybd_event(vkey, scancode, dwFlags, 0); + } + } +} + +UINT AwtRobot::HandleUnicodeKeys(jint key, DWORD dwFlags) +{ + NSWinInput::INPUT ip; + ip.type = 1; //INPUT_KEYBOARD; + ip.ki.wVk = 0; + ip.ki.wScan = key; + ip.ki.dwFlags = (DWORD)(dwFlags | 4); //KEYEVENTF_UNICODE(4) + ip.ki.dwExtraInfo = 0; + return SendInput(1, (LPINPUT)&ip, sizeof(INPUT)); } // @@ -444,3 +481,24 @@ CATCH_BAD_ALLOC; } + +JNIEXPORT void JNICALL Java_sun_awt_windows_WRobotPeer_keyPressUnicode( + JNIEnv *, jobject self, jint javakey ) +{ + TRY; + + AwtRobot::GetRobot(self)->KeyPressUnicode(javakey); + + CATCH_BAD_ALLOC; +} + +JNIEXPORT void JNICALL Java_sun_awt_windows_WRobotPeer_keyReleaseUnicode( + JNIEnv *, jobject self, jint javakey ) +{ + TRY; + + AwtRobot::GetRobot(self)->KeyReleaseUnicode(javakey); + + CATCH_BAD_ALLOC; +} +