5 // Created by Yifeng Huang on 7/26/12.
8 #import "NJOutputMouseMove.h"
10 #import "NJInputController.h"
12 @implementation NJOutputMouseMove
14 + (NSString *)serializationCode {
18 - (NSDictionary *)serialize {
19 return @{ @"type": self.class.serializationCode,
25 + (NJOutput *)outputWithSerialization:(NSDictionary *)serialization {
26 NJOutputMouseMove *output = [[NJOutputMouseMove alloc] init];
27 output.axis = [serialization[@"axis"] intValue];
28 output.speed = [serialization[@"speed"] floatValue];
34 - (BOOL)isContinuous {
38 #define CLAMP(a, l, h) MIN(h, MAX(a, l))
40 - (BOOL)update:(NJInputController *)ic {
41 if (self.magnitude < 0.05)
42 return NO; // dead zone
44 CGSize size = NSScreen.mainScreen.frame.size;
46 CGFloat dx = 0, dy = 0;
49 dx = -self.magnitude * _speed;
52 dx = self.magnitude * _speed;
55 dy = -self.magnitude * _speed;
58 dy = self.magnitude * _speed;
61 NSPoint mouseLoc = ic.mouseLoc;
62 mouseLoc.x = CLAMP(mouseLoc.x + dx, 0, size.width - 1);
63 mouseLoc.y = CLAMP(mouseLoc.y - dy, 0, size.height - 1);
64 ic.mouseLoc = mouseLoc;
66 CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
67 CGPointMake(mouseLoc.x, size.height - mouseLoc.y),
69 CGEventSetIntegerValueField(move, kCGMouseEventDeltaX, (int)dx);
70 CGEventSetIntegerValueField(move, kCGMouseEventDeltaY, (int)dy);
71 CGEventPost(kCGHIDEventTap, move);
73 if (CGEventSourceButtonState(kCGEventSourceStateHIDSystemState, kCGMouseButtonLeft)) {
74 CGEventSetType(move, kCGEventLeftMouseDragged);
75 CGEventSetIntegerValueField(move, kCGMouseEventButtonNumber, kCGMouseButtonLeft);
76 CGEventPost(kCGHIDEventTap, move);
78 if (CGEventSourceButtonState(kCGEventSourceStateHIDSystemState, kCGMouseButtonRight)) {
79 CGEventSetType(move, kCGEventRightMouseDragged);
80 CGEventSetIntegerValueField(move, kCGMouseEventButtonNumber, kCGMouseButtonRight);
81 CGEventPost(kCGHIDEventTap, move);
83 if (CGEventSourceButtonState(kCGEventSourceStateHIDSystemState, kCGMouseButtonCenter)) {
84 CGEventSetType(move, kCGEventOtherMouseDragged);
85 CGEventSetIntegerValueField(move, kCGMouseEventButtonNumber, kCGMouseButtonCenter);
86 CGEventPost(kCGHIDEventTap, move);