Skip to content

Commit 60314dd

Browse files
Copy on triple click
Instead of copy on double click, do copy on triple click because that's unlikely to be followed by an immediate paste, but does highlight eg browser url bar which is usually worth a copy.
1 parent 97bff2b commit 60314dd

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

macpaste.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,23 @@
2020
#include <AppKit/NSCursor.h>
2121

2222
char isDragging = 0;
23+
long long prevPrevClickTime = 0;
24+
long long prevClickTime = 0;
25+
long long curClickTime = 0;
2326
NSPoint initialLocation;
2427

2528
CGEventTapLocation tapA = kCGAnnotatedSessionEventTap;
2629
CGEventTapLocation tapH = kCGHIDEventTap;
2730

31+
#define TRIPLE_CLICK_MILLIS 500
32+
33+
long long now() {
34+
struct timeval te;
35+
gettimeofday( & te, NULL );
36+
long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // caculate milliseconds
37+
return milliseconds;
38+
}
39+
2840
static void paste(CGEventRef event) {
2941
// Mouse click to focus and position insertion cursor.
3042
CGPoint mouseLocation = CGEventGetLocation( event );
@@ -65,6 +77,16 @@ static void copy() {
6577
CFRelease( source );
6678
}
6779

80+
static void recordClickTime() {
81+
prevPrevClickTime = prevClickTime;
82+
prevClickTime = curClickTime;
83+
curClickTime = now();
84+
}
85+
86+
static char isTripleClick() {
87+
return ( curClickTime - prevPrevClickTime ) < TRIPLE_CLICK_MILLIS;
88+
}
89+
6890
static CGEventRef mouseCallback (
6991
CGEventTapProxy proxy,
7092
CGEventType type,
@@ -85,8 +107,17 @@ static CGEventRef mouseCallback (
85107
}
86108
break;
87109

110+
case kCGEventLeftMouseDown:
111+
//NSLog(@"down %@", NSStringFromPoint( [NSEvent mouseLocation]));
112+
recordClickTime();
113+
break;
114+
88115
case kCGEventLeftMouseUp:
89116
//NSLog(@"up %@", NSStringFromPoint( [NSEvent mouseLocation]));
117+
if (isTripleClick()) {
118+
NSLog(@"copytrlc %@", NSStringFromPoint( [NSEvent mouseLocation]));
119+
copy();
120+
}
90121
if (isDragging) {
91122
NSPoint clickLocation = [NSEvent mouseLocation];
92123
int xdiff = fabs(initialLocation.x-clickLocation.x);

0 commit comments

Comments
 (0)