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
241 {
242 CGEventRef event = CGEventCreateScrollWheelEvent(NULL,
243 kCGScrollEventUnitLine,
244 k_JAVA_ROBOT_WHEEL_COUNT, wheelAmt);
245
246 if (event != NULL) {
247 CGEventPost(kCGSessionEventTap, event);
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
242 {
243 CGEventRef event = CGEventCreateScrollWheelEvent(NULL,
244 kCGScrollEventUnitLine,
245 k_JAVA_ROBOT_WHEEL_COUNT, wheelAmt);
246
247 if (event != NULL) {
248 CGEventPost(kCGSessionEventTap, event);
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 CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
263
264 if ((javaKeyCode >= java_awt_event_KeyEvent_VK_0) && (javaKeyCode <= java_awt_event_KeyEvent_VK_9))
265 {
266 AXUIElementRef elem = AXUIElementCreateSystemWide();
267 AXUIElementPostKeyboardEvent(elem, (CGCharCode)0, keyCode, keyPressed);
268 CFRelease(elem);
269 } else {
270
271 /*
272 * JDK-8155740: AXUIElementPostKeyboardEvent posts correct key codes for
273 * number keys whereas CGEventPost posts Numpad keys for corresponding
274 * number key. Thereofore AXUIElementPostKeyboardEvent is used for posting
275 * numbers and CGEventCreateKeyboardEvent/CGEventPost is used for other
276 * keys.
277 * Key code for modifier key is required to distinguish between ALT and
278 * ALT-GR key for fixing issue 8155740.
279 */
280
281 CGEventRef event = CGEventCreateKeyboardEvent(NULL, keyCode, keyPressed);
282 if (event != NULL) {
283 CGEventPost(kCGSessionEventTap, event);
284 CFRelease(event);
285 }
286 }
287 }
288
289 /*
290 * Class: sun_lwawt_macosx_CRobot
291 * Method: nativeGetScreenPixels
292 * Signature: (IIIII[I)V
293 */
294 JNIEXPORT void JNICALL
295 Java_sun_lwawt_macosx_CRobot_nativeGetScreenPixels
296 (JNIEnv *env, jobject peer,
297 jint x, jint y, jint width, jint height, jintArray pixels)
298 {
299 JNF_COCOA_ENTER(env);
300
301 jint picX = x;
302 jint picY = y;
303 jint picWidth = width;
304 jint picHeight = height;
305
306 CGRect screenRect = CGRectMake(picX, picY, picWidth, picHeight);
|