< prev index next >

src/java.desktop/macosx/native/libawt_lwawt/awt/CRobot.m

Print this page




 249     }
 250 }
 251 
 252 /*
 253  * Class:     sun_lwawt_macosx_CRobot
 254  * Method:    keyEvent
 255  * Signature: (IZ)V
 256  */
 257 JNIEXPORT void JNICALL
 258 Java_sun_lwawt_macosx_CRobot_keyEvent
 259 (JNIEnv *env, jobject peer, jint javaKeyCode, jboolean keyPressed)
 260 {
 261     /*
 262      * Well, using CGEventCreateKeyboardEvent/CGEventPost would have been
 263      * a better solution, however, it gives me all kinds of trouble and I have
 264      * no idea how to solve them without inserting delays between simulated
 265      * events. So, I've ended up disabling it and opted for another approach
 266      * that uses Accessibility API instead.
 267      */
 268     CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
 269     AXUIElementRef elem = AXUIElementCreateSystemWide();
 270     AXUIElementPostKeyboardEvent(elem, (CGCharCode)0, keyCode, keyPressed);
 271     CFRelease(elem);
 272 










 273 
 274 #if 0
 275     CGEventRef event = CGEventCreateKeyboardEvent(NULL, keyCode, keyPressed);
 276     if (event != NULL) {
 277         CGEventPost(kCGSessionEventTap, event);
 278         CFRelease(event);
 279     }
 280 #endif
 281 }
 282 
 283 /*
 284  * Class:     sun_lwawt_macosx_CRobot
 285  * Method:    nativeGetScreenPixels
 286  * Signature: (IIIII[I)V
 287  */
 288 JNIEXPORT void JNICALL
 289 Java_sun_lwawt_macosx_CRobot_nativeGetScreenPixels
 290 (JNIEnv *env, jobject peer,
 291  jint x, jint y, jint width, jint height, jintArray pixels)
 292 {
 293     JNF_COCOA_ENTER(env);
 294 
 295     jint picX = x;
 296     jint picY = y;
 297     jint picWidth = width;
 298     jint picHeight = height;
 299 
 300     CGRect screenRect = CGRectMake(picX, picY, picWidth, picHeight);




 249     }
 250 }
 251 
 252 /*
 253  * Class:     sun_lwawt_macosx_CRobot
 254  * Method:    keyEvent
 255  * Signature: (IZ)V
 256  */
 257 JNIEXPORT void JNICALL
 258 Java_sun_lwawt_macosx_CRobot_keyEvent
 259 (JNIEnv *env, jobject peer, jint javaKeyCode, jboolean keyPressed)
 260 {
 261     /*
 262      * Well, using CGEventCreateKeyboardEvent/CGEventPost would have been
 263      * a better solution, however, it gives me all kinds of trouble and I have
 264      * no idea how to solve them without inserting delays between simulated
 265      * events. So, I've ended up disabling it and opted for another approach
 266      * that uses Accessibility API instead.
 267      */
 268     CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
 269 //    AXUIElementRef elem = AXUIElementCreateSystemWide();
 270 //    AXUIElementPostKeyboardEvent(elem, (CGCharCode)0, keyCode, keyPressed);
 271 //    CFRelease(elem);
 272 
 273         /*
 274      * JDK-8155740: AXUIElementPostKeyboardEvent posts 0 key code for all
 275      * the modifier keys with key codes (16, 17,18, 20, 157) and also for
 276      * newly added modifier key VK_ALT_GRAPH. But it posts correct key code
 277      * for all the other keys. On the other hand CGEventCreateKeyboardEvent
 278      * posts correct key code for all the keys and hence it is used to post
 279      * all the key events instead of AXUIElementPostKeyboardEvent.
 280      * Key code for modifier key is required to distinguish between ALT and
 281      * ALT-GR key for fixing issue 8155740.
 282      */
 283 
 284     CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
 285     CGEventRef event = CGEventCreateKeyboardEvent(source, keyCode, keyPressed);
 286     if (event != NULL) {
 287         CGEventPost(kCGSessionEventTap, event);
 288         CFRelease(event);
 289     }

 290 }
 291 
 292 /*
 293  * Class:     sun_lwawt_macosx_CRobot
 294  * Method:    nativeGetScreenPixels
 295  * Signature: (IIIII[I)V
 296  */
 297 JNIEXPORT void JNICALL
 298 Java_sun_lwawt_macosx_CRobot_nativeGetScreenPixels
 299 (JNIEnv *env, jobject peer,
 300  jint x, jint y, jint width, jint height, jintArray pixels)
 301 {
 302     JNF_COCOA_ENTER(env);
 303 
 304     jint picX = x;
 305     jint picY = y;
 306     jint picWidth = width;
 307     jint picHeight = height;
 308 
 309     CGRect screenRect = CGRectMake(picX, picY, picWidth, picHeight);


< prev index next >