< prev index next >

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

Print this page




 282         // stuff the 32-bit pixel back into the 32-bit RGBQUAD
 283         *prgbq = *( (RGBQUAD *)(&jpixel) );
 284     }
 285 
 286     // copy pixels into Java array
 287     env->SetIntArrayRegion(pixelArray, 0, numPixels, (jint *)pixelData);
 288     delete pinfo;
 289 
 290     // free all the GDI objects we made
 291     ::SelectObject(hdcMem, hOldBitmap);
 292     if (hOldPalette != NULL) {
 293         ::SelectPalette(hdcMem, hOldPalette, FALSE);
 294     }
 295     ::DeleteObject(hbitmap);
 296     ::DeleteDC(hdcMem);
 297     ::DeleteDC(hdcScreen);
 298 }
 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)


 427 
 428 JNIEXPORT void JNICALL Java_sun_awt_windows_WRobotPeer_keyPress(
 429   JNIEnv *, jobject self, jint javakey )
 430 {
 431     TRY;
 432 
 433     AwtRobot::GetRobot(self)->KeyPress(javakey);
 434 
 435     CATCH_BAD_ALLOC;
 436 }
 437 
 438 JNIEXPORT void JNICALL Java_sun_awt_windows_WRobotPeer_keyRelease(
 439   JNIEnv *, jobject self, jint javakey )
 440 {
 441     TRY;
 442 
 443     AwtRobot::GetRobot(self)->KeyRelease(javakey);
 444 
 445     CATCH_BAD_ALLOC;
 446 }























 282         // stuff the 32-bit pixel back into the 32-bit RGBQUAD
 283         *prgbq = *( (RGBQUAD *)(&jpixel) );
 284     }
 285 
 286     // copy pixels into Java array
 287     env->SetIntArrayRegion(pixelArray, 0, numPixels, (jint *)pixelData);
 288     delete pinfo;
 289 
 290     // free all the GDI objects we made
 291     ::SelectObject(hdcMem, hOldBitmap);
 292     if (hOldPalette != NULL) {
 293         ::SelectPalette(hdcMem, hOldPalette, FALSE);
 294     }
 295     ::DeleteObject(hbitmap);
 296     ::DeleteDC(hdcMem);
 297     ::DeleteDC(hdcScreen);
 298 }
 299 
 300 void AwtRobot::KeyPress( jint jkey )
 301 {
 302     DoKeyEvent(jkey, 0, false); // no flags means key down
 303 }
 304 
 305 void AwtRobot::KeyRelease( jint jkey )
 306 {
 307     DoKeyEvent(jkey, KEYEVENTF_KEYUP, false);
 308 }
 309 
 310 void AwtRobot::KeyPressUnicode( jint unicodeKey )
 311 {
 312     DoKeyEvent(unicodeKey, 0, true); // no flags means key down
 313 }
 314 
 315 void AwtRobot::KeyReleaseUnicode( jint unicodeKey )
 316 {
 317     DoKeyEvent(unicodeKey, KEYEVENTF_KEYUP, true);
 318 }
 319 
 320 void AwtRobot::DoKeyEvent( jint jkey, DWORD dwFlags, BOOL isUnicode )
 321 {
 322     UINT        vkey;
 323     UINT        modifiers;
 324     UINT        scancode;
 325     JNIEnv *    env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 326 
 327         if(isUnicode) {
 328         /*
 329            HandleUnicodeKeys() returns the status of the SendInput()
 330            which returns 0 if there is a fail else non-zero.
 331            The status 0 tells that the SendInput() in unable to interpret
 332            the supplied input upon which the illegal argument exception
 333            would be raised.
 334         */
 335         if(!HandleUnicodeKeys(jkey, dwFlags)) {
 336             // no equivalent Windows key found for given unicode key
 337             JNU_ThrowIllegalArgumentException(env, "Invalid unicode key");
 338         }
 339         }
 340         else {
 341             // convert Java key into Windows key (and modifiers too)
 342             AwtComponent::JavaKeyToWindowsKey(jkey, &vkey, &modifiers);
 343 
 344             if (vkey == 0) {
 345             // no equivalent Windows key found for given Java keycode
 346             JNU_ThrowIllegalArgumentException(env, "Invalid key code");
 347             } else {
 348                 // get the scancode from the virtual key
 349                 scancode = ::MapVirtualKey(vkey, 0);
 350                 if (vkey == VK_RMENU ||
 351                     vkey == VK_DELETE ||
 352                     vkey == VK_INSERT ||
 353                     vkey == VK_NEXT ||
 354                     vkey == VK_PRIOR ||
 355                     vkey == VK_HOME ||
 356                     vkey == VK_END ||
 357                     vkey == VK_LEFT ||
 358                     vkey == VK_RIGHT ||
 359                     vkey == VK_UP ||
 360                     vkey == VK_DOWN) {
 361                     dwFlags |= KEYEVENTF_EXTENDEDKEY;
 362                 }
 363                 keybd_event(vkey, scancode, dwFlags, 0);
 364             }
 365         }
 366 }
 367 
 368 UINT AwtRobot::HandleUnicodeKeys(jint unicodeKey, DWORD dwFlags)
 369 {
 370     NSWinInput::INPUT ip;
 371     ip.type = 1; //INPUT_KEYBOARD;
 372     ip.ki.wVk = 0;
 373     ip.ki.wScan = unicodeKey;
 374     ip.ki.dwFlags = (DWORD)(dwFlags | 4); //KEYEVENTF_UNICODE(4)
 375     ip.ki.dwExtraInfo = 0;
 376     return SendInput(1, (LPINPUT)&ip, sizeof(INPUT));
 377 }
 378 
 379 //
 380 // utility function to get the C++ object from the Java one
 381 //
 382 // (static)
 383 AwtRobot * AwtRobot::GetRobot( jobject self )
 384 {
 385     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 386     AwtRobot * robot = (AwtRobot *)JNI_GET_PDATA(self);
 387     DASSERT( !::IsBadWritePtr( robot, sizeof(AwtRobot)));
 388     return robot;
 389 }
 390 
 391 //////////////////////////////////////////////////////////////////////////////////////////////
 392 // Native method declarations
 393 //
 394 
 395 JNIEXPORT void JNICALL Java_sun_awt_windows_WRobotPeer_create(
 396     JNIEnv * env, jobject self)


 464 
 465 JNIEXPORT void JNICALL Java_sun_awt_windows_WRobotPeer_keyPress(
 466   JNIEnv *, jobject self, jint javakey )
 467 {
 468     TRY;
 469 
 470     AwtRobot::GetRobot(self)->KeyPress(javakey);
 471 
 472     CATCH_BAD_ALLOC;
 473 }
 474 
 475 JNIEXPORT void JNICALL Java_sun_awt_windows_WRobotPeer_keyRelease(
 476   JNIEnv *, jobject self, jint javakey )
 477 {
 478     TRY;
 479 
 480     AwtRobot::GetRobot(self)->KeyRelease(javakey);
 481 
 482     CATCH_BAD_ALLOC;
 483 }
 484 
 485 JNIEXPORT void JNICALL Java_sun_awt_windows_WRobotPeer_keyPressUnicode(
 486   JNIEnv *, jobject self, jint javakey )
 487 {
 488     TRY;
 489 
 490     AwtRobot::GetRobot(self)->KeyPressUnicode(javakey);
 491 
 492     CATCH_BAD_ALLOC;
 493 }
 494 
 495 JNIEXPORT void JNICALL Java_sun_awt_windows_WRobotPeer_keyReleaseUnicode(
 496   JNIEnv *, jobject self, jint javakey )
 497 {
 498     TRY;
 499 
 500     AwtRobot::GetRobot(self)->KeyReleaseUnicode(javakey);
 501 
 502     CATCH_BAD_ALLOC;
 503 }
 504 
< prev index next >