Send single/double/triple click information with mouse events.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Sat, 9 Mar 2013 12:34:58 +0000 (13:34 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Sat, 9 Mar 2013 12:34:58 +0000 (13:34 +0100)
Classes/NJOutputMouseButton.m

index d5b1f16e940baade744c9dddcaed2699decf49a3..18d82ff99b05fe76b41dfb9332f684a0138feadd 100644 (file)
@@ -7,7 +7,25 @@
 
 #import "NJOutputMouseButton.h"
 
-@implementation NJOutputMouseButton
+@implementation NJOutputMouseButton {
+    NSDate *upTime;
+    int clickCount;
+}
+
++ (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";
                                                eventType,
                                                CGPointMake(mouseLoc.x, height - mouseLoc.y),
                                                _button);
+
+    NSLog(@"%@\n%@", upTime, [NSDate date]);
+    if (clickCount >= 3 || [upTime compare:[NSDate date]] == NSOrderedAscending)
+        clickCount = 1;
+    else
+        ++clickCount;
+    CGEventSetIntegerValueField(click, kCGMouseEventClickState, clickCount);
+    
     CGEventPost(kCGHIDEventTap, click);
     CFRelease(click);
 }
                                                eventType,
                                                CGPointMake(mouseLoc.x, height - mouseLoc.y),
                                                _button);
+    CGEventSetIntegerValueField(click, kCGMouseEventClickState, clickCount);
     CGEventPost(kCGHIDEventTap, click);
     CFRelease(click);
+    upTime = [NJOutputMouseButton dateWithClickInterval];
 }
 
 @end