-
Notifications
You must be signed in to change notification settings - Fork 53
/
macpaste.c
183 lines (156 loc) · 5.19 KB
/
macpaste.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Public Domain License 2016
//
// Simulate right-handed unix/linux X11 middle-mouse-click copy and paste.
//
// References:
// http://stackoverflow.com/questions/3134901/mouse-tracking-daemon
// http://stackoverflow.com/questions/2379867/simulating-key-press-events-in-mac-os-x#2380280
//
// Compile with:
// gcc -framework ApplicationServices -o macpaste macpaste.c
//
// Start with:
// ./macpaste
//
// Terminate with Ctrl+C
#include <ApplicationServices/ApplicationServices.h>
#include <Carbon/Carbon.h> // kVK_ANSI_*
#include <sys/time.h> // gettimeofday
char isDragging = 0;
long long prevClickTime = 0;
long long curClickTime = 0;
CGEventTapLocation tapA = kCGAnnotatedSessionEventTap;
CGEventTapLocation tapH = kCGHIDEventTap;
#define DOUBLE_CLICK_MILLIS 500
long long now() {
struct timeval te;
gettimeofday( & te, NULL );
long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // caculate milliseconds
return milliseconds;
}
static void paste(CGEventRef event) {
// Mouse click to focus and position insertion cursor.
CGPoint mouseLocation = CGEventGetLocation( event );
CGEventRef mouseClickDown = CGEventCreateMouseEvent(
NULL, kCGEventLeftMouseDown, mouseLocation, kCGMouseButtonLeft );
CGEventRef mouseClickUp = CGEventCreateMouseEvent(
NULL, kCGEventLeftMouseUp, mouseLocation, kCGMouseButtonLeft );
CGEventPost( tapH, mouseClickDown );
CGEventPost( tapH, mouseClickUp );
CFRelease( mouseClickDown );
CFRelease( mouseClickUp );
// Allow click events time to position cursor before pasting.
usleep( 1000 );
// Paste.
CGEventSourceRef source = CGEventSourceCreate( kCGEventSourceStateCombinedSessionState );
CGEventRef kbdEventPasteDown = CGEventCreateKeyboardEvent( source, kVK_ANSI_V, 1 );
CGEventRef kbdEventPasteUp = CGEventCreateKeyboardEvent( source, kVK_ANSI_V, 0 );
CGEventSetFlags( kbdEventPasteDown, kCGEventFlagMaskCommand );
CGEventPost( tapA, kbdEventPasteDown );
CGEventPost( tapA, kbdEventPasteUp );
CFRelease( kbdEventPasteDown );
CFRelease( kbdEventPasteUp );
CFRelease( source );
}
static void copy() {
CGEventSourceRef source = CGEventSourceCreate( kCGEventSourceStateCombinedSessionState );
CGEventRef kbdEventDown = CGEventCreateKeyboardEvent( source, kVK_ANSI_C, 1 );
CGEventRef kbdEventUp = CGEventCreateKeyboardEvent( source, kVK_ANSI_C, 0 );
CGEventSetFlags( kbdEventDown, kCGEventFlagMaskCommand );
CGEventPost( tapA, kbdEventDown );
CGEventPost( tapA, kbdEventUp );
CFRelease( kbdEventDown );
CFRelease( kbdEventUp );
CFRelease( source );
}
static void recordClickTime() {
prevClickTime = curClickTime;
curClickTime = now();
}
static char isDoubleClickSpeed() {
return ( curClickTime - prevClickTime ) < DOUBLE_CLICK_MILLIS;
}
static char isDoubleClick() {
return isDoubleClickSpeed();
}
static CGEventRef mouseCallback (
CGEventTapProxy proxy,
CGEventType type,
CGEventRef event,
void * refcon
) {
int* dontpaste = refcon;
switch ( type )
{
case kCGEventOtherMouseDown:
if (*dontpaste == 0)
paste( event );
break;
case kCGEventLeftMouseDown:
recordClickTime();
break;
case kCGEventLeftMouseUp:
if ( isDoubleClick() || isDragging ) {
copy();
}
isDragging = 0;
break;
case kCGEventLeftMouseDragged:
isDragging = 1;
break;
default:
break;
}
// Pass on the event, we must not modify it anyway, we are a listener
return event;
}
int main (
int argc,
char ** argv
) {
CGEventMask emask;
CFMachPortRef myEventTap;
CFRunLoopSourceRef eventTapRLSrc;
// parse args for -n flag
int c;
int dontpaste = 0;
while ((c = getopt (argc, argv, "n")) != -1)
switch (c)
{
case 'n':
dontpaste = 1;
break;
default:
break;
}
// We want "other" mouse button click-release, such as middle or exotic.
emask = CGEventMaskBit( kCGEventOtherMouseDown ) |
CGEventMaskBit( kCGEventLeftMouseDown ) |
CGEventMaskBit( kCGEventLeftMouseUp ) |
CGEventMaskBit( kCGEventLeftMouseDragged );
// Create the Tap
myEventTap = CGEventTapCreate (
kCGSessionEventTap, // Catch all events for current user session
kCGTailAppendEventTap, // Append to end of EventTap list
kCGEventTapOptionListenOnly, // We only listen, we don't modify
emask,
& mouseCallback,
& dontpaste // dontpaste -> callback
);
// Create a RunLoop Source for it
eventTapRLSrc = CFMachPortCreateRunLoopSource(
kCFAllocatorDefault,
myEventTap,
0
);
// Add the source to the current RunLoop
CFRunLoopAddSource(
CFRunLoopGetCurrent(),
eventTapRLSrc,
kCFRunLoopDefaultMode
);
// Keep the RunLoop running forever
CFRunLoopRun();
// Not reached (RunLoop above never stops running)
return 0;
}