2 // NJOutputMouseButton.m
5 // Created by Yifeng Huang on 7/27/12.
8 #import "NJOutputMouseButton.h"
10 @implementation NJOutputMouseButton {
13 NSPoint clickPosition;
16 + (NSTimeInterval)doubleClickInterval {
17 static NSTimeInterval s_doubleClickThreshold;
18 if (!s_doubleClickThreshold) {
19 s_doubleClickThreshold = [[NSUserDefaults.standardUserDefaults
20 objectForKey:@"com.apple.mouse.doubleClickThreshold"] floatValue];
21 if (s_doubleClickThreshold <= 0)
22 s_doubleClickThreshold = 1.0;
24 return s_doubleClickThreshold;
27 + (NSDate *)dateWithClickInterval {
28 return [[NSDate alloc] initWithTimeIntervalSinceNow:self.doubleClickInterval];
31 + (NSString *)serializationCode {
32 return @"mouse button";
35 - (NSDictionary *)serialize {
36 return @{ @"type": self.class.serializationCode, @"button": @(_button) };
39 + (NJOutput *)outputDeserialize:(NSDictionary *)serialization
40 withMappings:(NSArray *)mappings {
41 NJOutputMouseButton *output = [[NJOutputMouseButton alloc] init];
42 output.button = [serialization[@"button"] intValue];
47 CGFloat height = NSScreen.mainScreen.frame.size.height;
48 NSPoint mouseLoc = NSEvent.mouseLocation;
49 CGEventType eventType = _button == kCGMouseButtonLeft ? kCGEventLeftMouseDown
50 : _button == kCGMouseButtonRight ? kCGEventRightMouseDown
51 : kCGEventOtherMouseDown;
52 CGEventRef click = CGEventCreateMouseEvent(NULL,
54 CGPointMake(mouseLoc.x, height - mouseLoc.y),
57 if (clickCount >= 3 || [upTime compare:[NSDate date]] == NSOrderedAscending
58 || !CGPointEqualToPoint(mouseLoc, clickPosition))
62 CGEventSetIntegerValueField(click, kCGMouseEventClickState, clickCount);
63 CGEventPost(kCGHIDEventTap, click);
65 clickPosition = mouseLoc;
69 upTime = [NJOutputMouseButton dateWithClickInterval];
70 NSPoint mouseLoc = NSEvent.mouseLocation;
71 CGFloat height = NSScreen.mainScreen.frame.size.height;
72 CGEventType eventType = _button == kCGMouseButtonLeft ? kCGEventLeftMouseUp
73 : _button == kCGMouseButtonRight ? kCGEventRightMouseUp
74 : kCGEventOtherMouseUp;
75 CGEventRef click = CGEventCreateMouseEvent(NULL,
77 CGPointMake(mouseLoc.x, height - mouseLoc.y),
79 CGEventSetIntegerValueField(click, kCGMouseEventClickState, clickCount);
80 CGEventPost(kCGHIDEventTap, click);