Skip to content

Commit 0c481d9

Browse files
Only copy on drag if we've dragged more than 5 px away
Prevent spurious copys on click while moving the mouse slightly
1 parent b4cc82d commit 0c481d9

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

macpaste.m

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
char isDragging = 0;
2323
long long prevClickTime = 0;
2424
long long curClickTime = 0;
25+
NSPoint initialLocation;
2526

2627
CGEventTapLocation tapA = kCGAnnotatedSessionEventTap;
2728
CGEventTapLocation tapH = kCGHIDEventTap;
@@ -102,22 +103,38 @@ static CGEventRef mouseCallback (
102103
button = CGEventGetIntegerValueField(event, kCGMouseEventButtonNumber);
103104
NSCursor* cursor = [NSCursor currentSystemCursor];
104105
NSCursor* ibeam = [NSCursor IBeamCursor];
105-
if (*dontpaste == 0 && button == 2 && NSEqualPoints([cursor hotSpot] , [ibeam hotSpot] ))
106+
if (*dontpaste == 0 && button == 2 && NSEqualPoints([cursor hotSpot] , [ibeam hotSpot] )) {
107+
// NSLog(@"paste %@", NSStringFromPoint( [NSEvent mouseLocation]));
106108
paste( event );
109+
}
107110
break;
108111

109112
case kCGEventLeftMouseDown:
113+
//NSLog(@"down %@", NSStringFromPoint( [NSEvent mouseLocation]));
110114
recordClickTime();
111115
break;
112116

113117
case kCGEventLeftMouseUp:
114-
if ( isDoubleClick() || isDragging ) {
118+
//NSLog(@"up %@", NSStringFromPoint( [NSEvent mouseLocation]));
119+
if (isDoubleClick()) {
120+
//NSLog(@"copydblc %@", NSStringFromPoint( [NSEvent mouseLocation]));
115121
copy();
116122
}
123+
if (isDragging) {
124+
NSPoint clickLocation = [NSEvent mouseLocation];
125+
int xdiff = fabs(initialLocation.x-clickLocation.x);
126+
int ydiff = fabs(initialLocation.y-clickLocation.y);
127+
if (xdiff > 5 || ydiff > 5) {
128+
//NSLog(@"copydrag %@ %@", NSStringFromPoint(initialLocation), NSStringFromPoint( [NSEvent mouseLocation]));
129+
copy();
130+
}
131+
}
117132
isDragging = 0;
118133
break;
119134

120135
case kCGEventLeftMouseDragged:
136+
if (!isDragging)
137+
initialLocation = [NSEvent mouseLocation];
121138
isDragging = 1;
122139
break;
123140

0 commit comments

Comments
 (0)