5 // Created by Yifeng Huang on 7/26/12.
8 #import "NJOutputMouseMove.h"
10 #import "NJDeviceController.h"
12 @implementation NJOutputMouseMove
14 + (NSString *)serializationCode {
18 - (NSDictionary *)serialize {
19 return @{ @"type": self.class.serializationCode,
25 + (NJOutput *)outputDeserialize:(NSDictionary *)serialization
26 withMappings:(NSArray *)mappings {
27 NJOutputMouseMove *output = [[NJOutputMouseMove alloc] init];
28 output.axis = [serialization[@"axis"] intValue];
29 output.speed = [serialization[@"speed"] floatValue];
35 - (BOOL)isContinuous {
39 #define CLAMP(a, l, h) MIN(h, MAX(a, l))
41 - (BOOL)update:(NJDeviceController *)jc {
42 if (self.magnitude < 0.05)
43 return NO; // dead zone
45 CGSize size = NSScreen.mainScreen.frame.size;
47 CGFloat dx = 0, dy = 0;
50 dx = -self.magnitude * _speed;
53 dx = self.magnitude * _speed;
56 dy = -self.magnitude * _speed;
59 dy = self.magnitude * _speed;
62 NSPoint mouseLoc = jc.mouseLoc;
63 mouseLoc.x = CLAMP(mouseLoc.x + dx, 0, size.width - 1);
64 mouseLoc.y = CLAMP(mouseLoc.y - dy, 0, size.height - 1);
65 jc.mouseLoc = mouseLoc;
67 CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
68 CGPointMake(mouseLoc.x, size.height - mouseLoc.y),
70 CGEventSetIntegerValueField(move, kCGMouseEventDeltaX, (int)dx);
71 CGEventSetIntegerValueField(move, kCGMouseEventDeltaY, (int)dy);
72 CGEventPost(kCGHIDEventTap, move);
74 if (CGEventSourceButtonState(kCGEventSourceStateHIDSystemState, kCGMouseButtonLeft)) {
75 CGEventSetType(move, kCGEventLeftMouseDragged);
76 CGEventSetIntegerValueField(move, kCGMouseEventButtonNumber, kCGMouseButtonLeft);
77 CGEventPost(kCGHIDEventTap, move);
79 if (CGEventSourceButtonState(kCGEventSourceStateHIDSystemState, kCGMouseButtonRight)) {
80 CGEventSetType(move, kCGEventRightMouseDragged);
81 CGEventSetIntegerValueField(move, kCGMouseEventButtonNumber, kCGMouseButtonRight);
82 CGEventPost(kCGHIDEventTap, move);
84 if (CGEventSourceButtonState(kCGEventSourceStateHIDSystemState, kCGMouseButtonCenter)) {
85 CGEventSetType(move, kCGEventOtherMouseDragged);
86 CGEventSetIntegerValueField(move, kCGMouseEventButtonNumber, kCGMouseButtonCenter);
87 CGEventPost(kCGHIDEventTap, move);