-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmacmouse.js
More file actions
237 lines (215 loc) · 8.6 KB
/
macmouse.js
File metadata and controls
237 lines (215 loc) · 8.6 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
'use strict'
var $ = require('NodObjC');
$.framework('Cocoa');
var pool;
var ptX = 0;
var ptY = 0;
/**
* Usage: mouse.init();
* Desc: Initializes the macmouse module
* Before: mouse is an uninitialized macmouse
* After: mouse is an initialized macmouse
*/
var init = function() {
pool = $.NSAutoreleasePool('alloc')('init');
var pos = getRealPos();
setPos(pos.x, pos.y);
}
/**
* Usage: var pos = mouse.getRealPos();
* Desc: Sends request for real mouse position, more expensive than getPos
* Before: mouse is an initialized macmouse
* After: pos holds x and y numbers representing the system mouse position
*/
var getRealPos = function() {
var pos = $.NSEvent("mouseLocation");
return { x: pos.x, y: pos.y };
}
/**
* Usage: var pos = mouse.getPos();
* Desc: Returns mouse position currently stored in the mouse module
* Before: mouse is an initialized macmouse
* After: pos holds x and y numbers representing the system mouse position currently stored in the
* mouse module
*/
var getPos = function() {
return { x: ptX, y: ptY };
}
// simple private helper function
var setPos = function(x, y) {
ptX = x;
ptY = y;
}
/**
* Usage: mouse.Place();
* Desc: Sends mouse event message to place the system mouse at a specific position
* Before: mouse is an initialized macmouse, x and y are numbers representing a specific position
* After: mouse event has been sent to move the system mouse to position defined by x and y
*/
var Place = function(x, y) {
setPos(x, y);
var moveEvent = $.CGEventCreateMouseEvent(null, $.kCGEventMouseMoved, $.CGPointMake(x, y), $.kCGMouseButtonLeft);
$.CGEventPost($.kCGHIDEventTap, moveEvent);
}
/**
* Usage: mouse.DragPlace(x, y);
* Desc: Sends mouse event message to place the system mouse at a specific position while in a
* dragging state
* Before: mouse is an initialized macmouse, x and y are numbers representing a specific position, the
* system mouse currently has (or thinks it has) the left mouse button pressed
* After: mouse event has been sent to move the system mouse to position defined by x and y with left
* mouse button pressed
*/
var DragPlace = function(x, y) {
setPos(x, y);
var moveEvent = $.CGEventCreateMouseEvent(null, $.kCGEventLeftMouseDragged, $.CGPointMake(x, y), $.kCGMouseButtonLeft);
$.CGEventPost($.kCGHIDEventTap, moveEvent);
}
/**
* Usage: mouse.Move(dx, dy);
* Desc: Sends mouse event message to move the system mouse (from current stored position in the mouse
* module) by a vector defined by dx and dy
* Before: mouse is an initialized macmouse, dx and dy are numbers representing our moving vector
* After: mouse event has been sent to move the system mouse by a vector defined by the numbers dx and dy
*/
var Move = function(dx, dy) {
ptX += dx;
ptY += dy;
var moveEvent = $.CGEventCreateMouseEvent(null, $.kCGEventMouseMoved, $.CGPointMake(ptX, ptY), $.kCGMouseButtonLeft);
$.CGEventPost($.kCGHIDEventTap, moveEvent);
}
/**
* Usage: mouse.DragMove(dx, dy);
* Desc: Sends mouse event message to move the system mouse (from current stored position in the mouse
* module) by a vector defined by dx and dy while in a dragging state
* Before: mouse is an initialized macmouse, dx and dy are numbers representing our moving vector, the
* system mouse currently has (or thinks it has) the left mouse button pressed
* After: mouse event has been sent to move the system mouse by a vector defined by the numbers dx and dy
* with left mouse button pressed
*/
var DragMove = function(dx, dy) {
ptX += dx;
ptY += dy;
var moveEvent = $.CGEventCreateMouseEvent(null, $.kCGEventLeftMouseDragged, $.CGPointMake(ptX, ptY), $.kCGMouseButtonLeft);
$.CGEventPost($.kCGHIDEventTap, moveEvent);
}
/**
* Usage: mouse.LeftButtonPress();
* Desc: Sends mouse event message to press and hold down the left button of the system mouse
* Before: mouse is an initialized macmouse
* After: mouse event has been sent to press and hold the left button on the system mouse
*/
var LeftButtonPress = function() {
var clickDown = $.CGEventCreateMouseEvent(null, $.kCGEventLeftMouseDown, $.CGPointMake(ptX, ptY), $.kCGMouseButtonLeft);
$.CGEventPost($.kCGHIDEventTap, clickDown);
}
/**
* Usage: mouse.LeftButtonRelease();
* Desc: Sends mouse event message to release a pressed left button of the system mouse
* Before: mouse is an initialized macmouse
* After: mouse event has been sent to release a pressed left button on the system mouse
*/
var LeftButtonRelease = function() {
var clickUp = $.CGEventCreateMouseEvent(null, $.kCGEventLeftMouseUp, $.CGPointMake(ptX, ptY), $.kCGMouseButtonLeft);
$.CGEventPost($.kCGHIDEventTap, clickUp);
}
/**
* Usage: mouse.Click();
* Desc: Sends mouse event message to press and release left button of the system mouse
* Before: mouse is an initialized macmouse
* After: mouse event has been sent to press and release left button on the system mouse
*/
var Click = function() {
LeftButtonPress();
LeftButtonRelease();
}
/**
* Usage: mouse.RightButtonPress();
* Desc: Sends mouse event message to press and hold down the right button of the system mouse
* Before: mouse is an initialized macmouse
* After: mouse event has been sent to press and hold the right button on the system mouse
*/
var RightButtonPress = function() {
var clickDown = $.CGEventCreateMouseEvent(null, $.kCGEventRightMouseDown, $.CGPointMake(ptX, ptY), $.kCGEventRightMouseDown);
$.CGEventPost($.kCGHIDEventTap, clickDown);
}
/**
* Usage: mouse.RightButtonRelease();
* Desc: Sends mouse event message to release a pressed right button of the system mouse
* Before: mouse is an initialized macmouse
* After: mouse event has been sent to release a pressed right button on the system mouse
*/
var RightButtonRelease = function() {
var clickUp = $.CGEventCreateMouseEvent(null, $.kCGEventRightMouseUp, $.CGPointMake(ptX, ptY), $.kCGEventRightMouseDown);
$.CGEventPost($.kCGHIDEventTap, clickUp);
}
/**
* Usage: mouse.RightClick();
* Desc: Sends mouse event message to press and release right button of the system mouse
* Before: mouse is an initialized macmouse
* After: mouse event has been sent to press and release right button on the system mouse
*/
var RightClick = function() {
RightButtonPress();
RightButtonRelease();
}
/**
* Usage: mouse.DoubleClick();
* Desc: Sends mouse event message to double click the system mouse
* Before: mouse is an initialized macmouse
* After: mouse event has been sent to double click the system mouse
*/
var DoubleClick = function() {
var evt = $.CGEventCreateMouseEvent(null, $.kCGEventLeftMouseDown, $.CGPointMake(ptX, ptY), $.kCGMouseButtonLeft);
$.CGEventSetIntegerValueField(evt, $.kCGMouseEventClickState, 2);
$.CGEventPost($.kCGHIDEventTap, evt);
$.CGEventSetType(evt, $.kCGEventLeftMouseUp);
$.CGEventPost($.kCGHIDEventTap, evt);
$.CGEventSetType(evt, $.kCGEventLeftMouseDown);
$.CGEventPost($.kCGHIDEventTap, evt);
$.CGEventSetType(evt, $.kCGEventLeftMouseUp);
$.CGEventPost($.kCGHIDEventTap, evt);
}
/**
* Usage: mouse.Scroll(vertical, horizontal);
* Desc: Sends mouse scroll event message
* Before: mouse is an initialized macmouse, vertical and horizontal
* are 'small signed integer values, typically in a range from -10 to +10',
* in reality they can be any integer from -32768 to 32767,
* if horizontal isn't provided it defaults to 0
* After: scroll event has been sent to scroll by a vector defined by the
* vertical and horizontal integers
*/
var Scroll = function(vertical, horizontal) {
if (typeof horizontal === 'undefined') horizontal = 0;
var scrollEvent = $.CGEventCreateScrollWheelEvent(null, $.kCGScrollEventUnitLine, 1, vertical, horizontal);
$.CGEventPost($.kCGHIDEventTap, scrollEvent);
}
/**
* Usage: mouse.quit();
* Desc: Does garbage collection some on objective c stuff, be a good lad and call this when
* you're done using the macmouse module
* Before: mouse is an initialized macmouse
* After: mouse is an uninitialized macmouse
*/
var quit = function() {
pool('drain');
}
module.exports = {
init: init,
getRealPos: getRealPos,
getPos: getPos,
Place: Place,
DragPlace: DragPlace,
Move: Move,
DragMove: DragMove,
LeftButtonPress: LeftButtonPress,
LeftButtonRelease: LeftButtonRelease,
Click: Click,
RightButtonPress: RightButtonPress,
RightButtonRelease: RightButtonRelease,
RightClick: RightClick,
DoubleClick: DoubleClick,
Scroll: Scroll,
quit: quit
}