< prev index next >

src/java.desktop/windows/native/libawt/windows/awt_Robot.cpp

Print this page




 299 
 300 void AwtRobot::KeyPress( jint jkey )
 301 {
 302     DoKeyEvent(jkey, 0); // no flags means key down
 303 }
 304 
 305 void AwtRobot::KeyRelease( jint jkey )
 306 {
 307     DoKeyEvent(jkey, KEYEVENTF_KEYUP);
 308 }
 309 
 310 void AwtRobot::DoKeyEvent( jint jkey, DWORD dwFlags )
 311 {
 312     UINT        vkey;
 313     UINT        modifiers;
 314     UINT        scancode;
 315     JNIEnv *    env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 316 
 317     // convert Java key into Windows key (and modifiers too)
 318     AwtComponent::JavaKeyToWindowsKey(jkey, &vkey, &modifiers);

 319     if (vkey == 0) {














 320         // no equivalent Windows key found for given Java keycode
 321         JNU_ThrowIllegalArgumentException(env, "Invalid key code");

 322     } else {
 323         // get the scancode from the virtual key
 324         scancode = ::MapVirtualKey(vkey, 0);
 325         if (vkey == VK_RMENU ||
 326             vkey == VK_DELETE ||
 327             vkey == VK_INSERT ||
 328             vkey == VK_NEXT ||
 329             vkey == VK_PRIOR ||
 330             vkey == VK_HOME ||
 331             vkey == VK_END ||
 332             vkey == VK_LEFT ||
 333             vkey == VK_RIGHT ||
 334             vkey == VK_UP ||
 335             vkey == VK_DOWN) {
 336             dwFlags |= KEYEVENTF_EXTENDEDKEY;
 337         }
 338         keybd_event(vkey, scancode, dwFlags, 0);
 339     }











 340 }
 341 
 342 //
 343 // utility function to get the C++ object from the Java one
 344 //
 345 // (static)
 346 AwtRobot * AwtRobot::GetRobot( jobject self )
 347 {
 348     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 349     AwtRobot * robot = (AwtRobot *)JNI_GET_PDATA(self);
 350     DASSERT( !::IsBadWritePtr( robot, sizeof(AwtRobot)));
 351     return robot;
 352 }
 353 
 354 //////////////////////////////////////////////////////////////////////////////////////////////
 355 // Native method declarations
 356 //
 357 
 358 JNIEXPORT void JNICALL Java_sun_awt_windows_WRobotPeer_create(
 359     JNIEnv * env, jobject self)




 299 
 300 void AwtRobot::KeyPress( jint jkey )
 301 {
 302     DoKeyEvent(jkey, 0); // no flags means key down
 303 }
 304 
 305 void AwtRobot::KeyRelease( jint jkey )
 306 {
 307     DoKeyEvent(jkey, KEYEVENTF_KEYUP);
 308 }
 309 
 310 void AwtRobot::DoKeyEvent( jint jkey, DWORD dwFlags )
 311 {
 312     UINT        vkey;
 313     UINT        modifiers;
 314     UINT        scancode;
 315     JNIEnv *    env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 316 
 317     // convert Java key into Windows key (and modifiers too)
 318     AwtComponent::JavaKeyToWindowsKey(jkey, &vkey, &modifiers);
 319 
 320     if (vkey == 0) {
 321         /* vkey would be 0 for all non-ascii inputs. If non-ascii
 322            then we assume they are unicode characters and will
 323            supply such input to SendInput() which can handle unicode
 324            characters as well as ascii chars. Windows provides api's to
 325            handle ascii and unicode characters. All ascii characters
 326            (both OEM and standard ascii) would be supplied to keybd_event().
 327 
 328            HandleUnicodeKeys() returns the status of the SendInput()
 329            which returns 0 if there is a fail else non-zero.
 330            The status 0 tells that the SendInput() in unable to interpret
 331            the supplied input upon which the illegal argument exception
 332            would be raised.
 333         */
 334         if(!HandleUnicodeKeys(jkey, dwFlags)) {
 335             // no equivalent Windows key found for given Java keycode
 336             JNU_ThrowIllegalArgumentException(env, "Invalid key code");
 337         }
 338     } else {
 339         // get the scancode from the virtual key
 340         scancode = ::MapVirtualKey(vkey, 0);
 341         if (vkey == VK_RMENU ||
 342             vkey == VK_DELETE ||
 343             vkey == VK_INSERT ||
 344             vkey == VK_NEXT ||
 345             vkey == VK_PRIOR ||
 346             vkey == VK_HOME ||
 347             vkey == VK_END ||
 348             vkey == VK_LEFT ||
 349             vkey == VK_RIGHT ||
 350             vkey == VK_UP ||
 351             vkey == VK_DOWN) {
 352             dwFlags |= KEYEVENTF_EXTENDEDKEY;
 353         }
 354         keybd_event(vkey, scancode, dwFlags, 0);
 355     }
 356 }
 357 
 358 UINT AwtRobot::HandleUnicodeKeys(jint key, DWORD dwFlags)
 359 {
 360     NSWinInput::INPUT ip;
 361     ip.type = 1; //INPUT_KEYBOARD;
 362     ip.ki.wVk = 0;
 363     ip.ki.wScan = key;
 364     ip.ki.dwFlags = (DWORD)(dwFlags | 4); //KEYEVENTF_UNICODE(4)
 365     ip.ki.dwExtraInfo = 0;
 366     return SendInput(1, (LPINPUT)&ip, sizeof(INPUT));
 367 }
 368 
 369 //
 370 // utility function to get the C++ object from the Java one
 371 //
 372 // (static)
 373 AwtRobot * AwtRobot::GetRobot( jobject self )
 374 {
 375     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 376     AwtRobot * robot = (AwtRobot *)JNI_GET_PDATA(self);
 377     DASSERT( !::IsBadWritePtr( robot, sizeof(AwtRobot)));
 378     return robot;
 379 }
 380 
 381 //////////////////////////////////////////////////////////////////////////////////////////////
 382 // Native method declarations
 383 //
 384 
 385 JNIEXPORT void JNICALL Java_sun_awt_windows_WRobotPeer_create(
 386     JNIEnv * env, jobject self)


< prev index next >