< prev index next >

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

Print this page




  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 
  27 #import "jni_util.h"
  28 
  29 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  30 #import <ApplicationServices/ApplicationServices.h>
  31 
  32 #import "CRobotKeyCode.h"
  33 #import "LWCToolkit.h"
  34 #import "sun_lwawt_macosx_CRobot.h"
  35 #import "java_awt_event_InputEvent.h"

  36 #import "sizecalc.h"
  37 
  38 // Starting number for event numbers generated by Robot.
  39 // Apple docs don't mention at all what are the requirements
  40 // for these numbers. It seems that they must be higher
  41 // than event numbers from real events, which start at some
  42 // value close to zero. There is no API for obtaining current
  43 // event number, so we have to start from some random number.
  44 // 32000 as starting value works for me, let's hope that it will
  45 // work for others as well.
  46 #define ROBOT_EVENT_NUMBER_START 32000
  47 
  48 #define k_JAVA_ROBOT_WHEEL_COUNT 1
  49 
  50 #if !defined(kCGBitmapByteOrder32Host)
  51 #define kCGBitmapByteOrder32Host 0
  52 #endif
  53 
  54 // In OS X, left and right mouse button share the same click count.
  55 // That is, if one starts clicking the left button rapidly and then


 248         CFRelease(event);
 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);




  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 
  27 #import "jni_util.h"
  28 
  29 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  30 #import <ApplicationServices/ApplicationServices.h>
  31 
  32 #import "CRobotKeyCode.h"
  33 #import "LWCToolkit.h"
  34 #import "sun_lwawt_macosx_CRobot.h"
  35 #import "java_awt_event_InputEvent.h"
  36 #import "java_awt_event_KeyEvent.h"
  37 #import "sizecalc.h"
  38 
  39 // Starting number for event numbers generated by Robot.
  40 // Apple docs don't mention at all what are the requirements
  41 // for these numbers. It seems that they must be higher
  42 // than event numbers from real events, which start at some
  43 // value close to zero. There is no API for obtaining current
  44 // event number, so we have to start from some random number.
  45 // 32000 as starting value works for me, let's hope that it will
  46 // work for others as well.
  47 #define ROBOT_EVENT_NUMBER_START 32000
  48 
  49 #define k_JAVA_ROBOT_WHEEL_COUNT 1
  50 
  51 #if !defined(kCGBitmapByteOrder32Host)
  52 #define kCGBitmapByteOrder32Host 0
  53 #endif
  54 
  55 // In OS X, left and right mouse button share the same click count.
  56 // That is, if one starts clicking the left button rapidly and then


 249         CFRelease(event);
 250     }
 251 }
 252 
 253 /*
 254  * Class:     sun_lwawt_macosx_CRobot
 255  * Method:    keyEvent
 256  * Signature: (IZ)V
 257  */
 258 JNIEXPORT void JNICALL
 259 Java_sun_lwawt_macosx_CRobot_keyEvent
 260 (JNIEnv *env, jobject peer, jint javaKeyCode, jboolean keyPressed)
 261 {
 262     /*
 263      * Well, using CGEventCreateKeyboardEvent/CGEventPost would have been
 264      * a better solution, however, it gives me all kinds of trouble and I have
 265      * no idea how to solve them without inserting delays between simulated
 266      * events. So, I've ended up disabling it and opted for another approach
 267      * that uses Accessibility API instead.
 268      */
 269 
 270     CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
 271 
 272     /*
 273      * JDK-8155740: AXUIElementPostKeyboardEvent posts 0 key code for all
 274      * the modifier keys with key codes (16, 17,18, 20, 157) and also for
 275      * newly added modifier key VK_ALT_GRAPH. But it posts correct key code
 276      * for all the other keys. On the other hand CGEventCreateKeyboardEvent
 277      * posts correct key code for all the keys except numeric keys (0 to 9).
 278      * CGEventCreateKeyboardEvent posts wrong key codes for the number keys
 279      * 0 to 9. Instead of posting number key codes it posts Numpad key codes
 280      * for the corresponding number key. For example Numpad0 key is posted for
 281      * number 0 and simillarly for remaining num keys. Based on this behaviour
 282      * AXUIElementPostKeyboardEvent is used to post only numeric key events
 283      * and CGEventCreateKeyboardEvent used for posting all other key events.
 284      */
 285 
 286     if ((javaKeyCode >= java_awt_event_KeyEvent_VK_0) &&
 287         (javaKeyCode <= java_awt_event_KeyEvent_VK_9))
 288     {
 289         AXUIElementRef elem = AXUIElementCreateSystemWide();
 290         AXUIElementPostKeyboardEvent(elem, (CGCharCode)0, keyCode, keyPressed);
 291         CFRelease(elem);
 292     } else {


 293         CGEventRef event = CGEventCreateKeyboardEvent(NULL, keyCode, keyPressed);
 294         if (event != NULL) {
 295             CGEventPost(kCGSessionEventTap, event);
 296             CFRelease(event);
 297         }
 298     }
 299 }
 300 
 301 /*
 302  * Class:     sun_lwawt_macosx_CRobot
 303  * Method:    nativeGetScreenPixels
 304  * Signature: (IIIII[I)V
 305  */
 306 JNIEXPORT void JNICALL
 307 Java_sun_lwawt_macosx_CRobot_nativeGetScreenPixels
 308 (JNIEnv *env, jobject peer,
 309  jint x, jint y, jint width, jint height, jintArray pixels)
 310 {
 311     JNF_COCOA_ENTER(env);
 312 
 313     jint picX = x;
 314     jint picY = y;
 315     jint picWidth = width;
 316     jint picHeight = height;
 317 
 318     CGRect screenRect = CGRectMake(picX, picY, picWidth, picHeight);


< prev index next >