X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=Classes%2FNJOutputMouseButton.m;h=0b039d108d2eaef2821eb6e2ba94b9cff6e28cb0;hp=d5b1f16e940baade744c9dddcaed2699decf49a3;hb=24bdb92798b9abe86c7954042a47523791736b7c;hpb=0064c1fbff36795885a9724081af2a17d83c20a3 diff --git a/Classes/NJOutputMouseButton.m b/Classes/NJOutputMouseButton.m index d5b1f16..0b039d1 100644 --- a/Classes/NJOutputMouseButton.m +++ b/Classes/NJOutputMouseButton.m @@ -7,7 +7,26 @@ #import "NJOutputMouseButton.h" -@implementation NJOutputMouseButton +@implementation NJOutputMouseButton { + NSDate *upTime; + int clickCount; + NSPoint clickPosition; +} + ++ (NSTimeInterval)doubleClickInterval { + static NSTimeInterval s_doubleClickThreshold; + if (!s_doubleClickThreshold) { + s_doubleClickThreshold = [[NSUserDefaults.standardUserDefaults + objectForKey:@"com.apple.mouse.doubleClickThreshold"] floatValue]; + if (s_doubleClickThreshold <= 0) + s_doubleClickThreshold = 1.0; + } + return s_doubleClickThreshold; +} + ++ (NSDate *)dateWithClickInterval { + return [[NSDate alloc] initWithTimeIntervalSinceNow:self.doubleClickInterval]; +} + (NSString *)serializationCode { return @"mouse button"; @@ -17,8 +36,7 @@ return @{ @"type": self.class.serializationCode, @"button": @(_button) }; } -+ (NJOutput *)outputDeserialize:(NSDictionary *)serialization - withMappings:(NSArray *)mappings { ++ (NJOutput *)outputDeserialize:(NSDictionary *)serialization { NJOutputMouseButton *output = [[NJOutputMouseButton alloc] init]; output.button = [serialization[@"button"] intValue]; return output; @@ -27,23 +45,37 @@ - (void)trigger { CGFloat height = NSScreen.mainScreen.frame.size.height; NSPoint mouseLoc = NSEvent.mouseLocation; - CGEventType eventType = (_button == kCGMouseButtonLeft) ? kCGEventLeftMouseDown : kCGEventRightMouseDown; + CGEventType eventType = _button == kCGMouseButtonLeft ? kCGEventLeftMouseDown + : _button == kCGMouseButtonRight ? kCGEventRightMouseDown + : kCGEventOtherMouseDown; CGEventRef click = CGEventCreateMouseEvent(NULL, eventType, CGPointMake(mouseLoc.x, height - mouseLoc.y), _button); + + if (clickCount >= 3 || [upTime compare:[NSDate date]] == NSOrderedAscending + || !CGPointEqualToPoint(mouseLoc, clickPosition)) + clickCount = 1; + else + ++clickCount; + CGEventSetIntegerValueField(click, kCGMouseEventClickState, clickCount); CGEventPost(kCGHIDEventTap, click); CFRelease(click); + clickPosition = mouseLoc; } - (void)untrigger { - CGFloat height = NSScreen.mainScreen.frame.size.height; + upTime = [NJOutputMouseButton dateWithClickInterval]; NSPoint mouseLoc = NSEvent.mouseLocation; - CGEventType eventType = (_button == kCGMouseButtonLeft) ? kCGEventLeftMouseUp : kCGEventRightMouseUp; + CGFloat height = NSScreen.mainScreen.frame.size.height; + CGEventType eventType = _button == kCGMouseButtonLeft ? kCGEventLeftMouseUp + : _button == kCGMouseButtonRight ? kCGEventRightMouseUp + : kCGEventOtherMouseUp; CGEventRef click = CGEventCreateMouseEvent(NULL, eventType, CGPointMake(mouseLoc.x, height - mouseLoc.y), _button); + CGEventSetIntegerValueField(click, kCGMouseEventClickState, clickCount); CGEventPost(kCGHIDEventTap, click); CFRelease(click); }