├── .gitignore ├── LICENSE ├── README.md ├── macmouse.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Deployed apps should consider commenting this line out: 24 | # see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 25 | node_modules 26 | 27 | # mac hidden file 28 | .DS_Store 29 | 30 | # Sublime Text files 31 | macmouse.sublime-project 32 | macmouse.sublime-workspace 33 | 34 | # ignore test file 35 | test.js -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Sveinn Flóki Guðmundsson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # macmouse 2 | 3 | [![NPM](https://nodei.co/npm/macmouse.png)](https://www.npmjs.com/package/macmouse) 4 | 5 | A node.js module that enables you to create virtual mouse input on Mac OS X. 6 | 7 | ## Credits 8 | Uses [NodObjC](https://github.com/TooTallNate/NodObjC) to hook into the Cocoa framework. The macmouse module is merely a wrapper around mouse control commands to the Cocoa framework via NodObjC. 9 | 10 | ## Installation 11 | 12 | Install using `npm`, 13 | 14 | ``` bash 15 | $ npm install macmouse 16 | ``` 17 | 18 | ## Usage Example 19 | ``` javascript 20 | var mouse = require('macmouse'); 21 | 22 | mouse.init(); 23 | 24 | var ptX = 800; 25 | var ptY = 600; 26 | 27 | var counter = 0; 28 | 29 | var doThings = function() { 30 | mouse.Place(ptX, ptY); 31 | setTimeout(pressAndHold, 250); 32 | } 33 | 34 | var pressAndHold = function() { 35 | mouse.LeftButtonPress(); 36 | setTimeout(doDragStuff, 250); 37 | } 38 | 39 | var doDragStuff = function() { 40 | ptX += 2; 41 | ptY += 2; 42 | mouse.DragPlace(ptX, ptY); 43 | if (counter < 15) { 44 | counter += 1; 45 | setTimeout(doDragStuff, 250); 46 | } 47 | else { 48 | mouse.LeftButtonRelease(); 49 | } 50 | } 51 | 52 | 53 | doThings(); 54 | 55 | mouse.quit(); 56 | 57 | ``` 58 | 59 | ## List of functions 60 | 61 | And a small description for each function. 62 | 63 | ``` javascript 64 | // Desc: Imports the macmouse module as mouse 65 | // Before: nothing 66 | // After: mouse is an uninitialized macmouse 67 | var mouse = require('macmouse'); 68 | ``` 69 | 70 | ### init 71 | 72 | ``` javascript 73 | // Desc: Initializes the macmouse module 74 | // Before: mouse is an uninitialized macmouse 75 | // After: mouse is an initialized macmouse 76 | mouse.init(); 77 | ``` 78 | 79 | ### getRealPos 80 | 81 | ``` javascript 82 | // Desc: Sends request for real mouse position, more expensive than getPos 83 | // Before: mouse is an initialized macmouse 84 | // After: pos holds x and y numbers representing the system mouse position 85 | var pos = mouse.getRealPos(); 86 | var x = pos.x; 87 | var y = pos.y; 88 | ``` 89 | 90 | ### getPos 91 | 92 | ``` javascript 93 | // Desc: Returns mouse position currently stored in the mouse module 94 | // Before: mouse is an initialized macmouse 95 | // After: pos holds x and y numbers representing the system mouse position currently stored in the 96 | // mouse module 97 | var pos = mouse.getPos(); 98 | var x = pos.x; 99 | var y = pos.y; 100 | ``` 101 | 102 | ### Place 103 | 104 | ``` javascript 105 | // Desc: Sends mouse event message to place the system mouse at a specific position 106 | // Before: mouse is an initialized macmouse, x and y are numbers representing a specific position 107 | // After: mouse event has been sent to move the system mouse to position defined by x and y 108 | mouse.Place(x, y); 109 | ``` 110 | 111 | ### DragPlace 112 | 113 | ``` javascript 114 | // Desc: Sends mouse event message to place the system mouse at a specific position while in a 115 | // dragging state 116 | // Before: mouse is an initialized macmouse, x and y are numbers representing a specific position, the 117 | // system mouse currently has (or thinks it has) the left mouse button pressed 118 | // After: mouse event has been sent to move the system mouse to position defined by x and y with left 119 | // mouse button pressed 120 | mouse.DragPlace(x, y); 121 | ``` 122 | 123 | ### Move 124 | 125 | ``` javascript 126 | // Desc: Sends mouse event message to move the system mouse (from current stored position in the mouse 127 | // module) by a vector defined by dx and dy 128 | // Before: mouse is an initialized macmouse, dx and dy are numbers representing our moving vector 129 | // After: mouse event has been sent to move the system mouse by a vector defined by the numbers dx and dy 130 | mouse.Move(dx, dy); 131 | ``` 132 | 133 | ### DragMove 134 | 135 | ``` javascript 136 | // Desc: Sends mouse event message to move the system mouse (from current stored position in the mouse 137 | // module) by a vector defined by dx and dy while in a dragging state 138 | // Before: mouse is an initialized macmouse, dx and dy are numbers representing our moving vector, the 139 | // system mouse currently has (or thinks it has) the left mouse button pressed 140 | // After: mouse event has been sent to move the system mouse by a vector defined by the numbers dx and dy 141 | // with left mouse button pressed 142 | mouse.DragMove(dx, dy); 143 | ``` 144 | 145 | ### LeftButtonPress 146 | 147 | ``` javascript 148 | // Desc: Sends mouse event message to press and hold down the left button of the system mouse 149 | // Before: mouse is an initialized macmouse 150 | // After: mouse event has been sent to press and hold the left button on the system mouse 151 | mouse.LeftButtonPress(); 152 | ``` 153 | 154 | ### LeftButtonRelease 155 | 156 | ``` javascript 157 | // Desc: Sends mouse event message to release a pressed left button of the system mouse 158 | // Before: mouse is an initialized macmouse 159 | // After: mouse event has been sent to release a pressed left button on the system mouse 160 | mouse.LeftButtonRelease(); 161 | ``` 162 | 163 | ### Click 164 | 165 | ``` javascript 166 | // Desc: Sends mouse event message to press and release left button of the system mouse 167 | // Before: mouse is an initialized macmouse 168 | // After: mouse event has been sent to press and release left button on the system mouse 169 | mouse.Click(); 170 | ``` 171 | 172 | ### RightButtonPress 173 | 174 | ``` javascript 175 | // Desc: Sends mouse event message to press and hold down the right button of the system mouse 176 | // Before: mouse is an initialized macmouse 177 | // After: mouse event has been sent to press and hold the right button on the system mouse 178 | mouse.RightButtonPress(); 179 | ``` 180 | 181 | ### RightButtonRelease 182 | 183 | ``` javascript 184 | // Desc: Sends mouse event message to release a pressed right button of the system mouse 185 | // Before: mouse is an initialized macmouse 186 | // After: mouse event has been sent to release a pressed right button on the system mouse 187 | mouse.RightButtonRelease(); 188 | ``` 189 | 190 | ### RightClick 191 | 192 | ``` javascript 193 | // Desc: Sends mouse event message to press and release right button of the system mouse 194 | // Before: mouse is an initialized macmouse 195 | // After: mouse event has been sent to press and release right button on the system mouse 196 | mouse.RightClick(); 197 | ``` 198 | 199 | ### DoubleClick 200 | 201 | ``` javascript 202 | // Desc: Sends mouse event message to double click the system mouse 203 | // Before: mouse is an initialized macmouse 204 | // After: mouse event has been sent to double click the system mouse 205 | mouse.DoubleClick(); 206 | ``` 207 | 208 | ### Scroll 209 | 210 | ``` javascript 211 | // Desc: Sends mouse scroll event message 212 | // Before: mouse is an initialized macmouse, vertical and horizontal 213 | // are 'small signed integer values, typically in a range from -10 to +10', 214 | // in reality they can be any integer from -32768 to 32767, 215 | // if horizontal isn't provided it defaults to 0 216 | // After: scroll event has been sent to scroll by a vector defined by the 217 | // vertical and horizontal integers 218 | mouse.Scroll(vertical); 219 | mouse.Scroll(vertical, horizontal); 220 | ``` 221 | 222 | ### quit 223 | 224 | ``` javascript 225 | // Desc: Does garbage collection on some objective c stuff, be a good lad and call this when 226 | // you're done using the macmouse module 227 | // Before: mouse is an initialized macmouse 228 | // After: mouse is an uninitialized macmouse 229 | mouse.quit(); 230 | ``` 231 | 232 | ## License 233 | (MIT License) 234 | -------------------------------------------------------------------------------- /macmouse.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var $ = require('NodObjC'); 4 | $.framework('Cocoa'); 5 | 6 | var pool; 7 | 8 | var ptX = 0; 9 | var ptY = 0; 10 | 11 | 12 | /** 13 | * Usage: mouse.init(); 14 | * Desc: Initializes the macmouse module 15 | * Before: mouse is an uninitialized macmouse 16 | * After: mouse is an initialized macmouse 17 | */ 18 | var init = function() { 19 | pool = $.NSAutoreleasePool('alloc')('init'); 20 | var pos = getRealPos(); 21 | setPos(pos.x, pos.y); 22 | } 23 | 24 | /** 25 | * Usage: var pos = mouse.getRealPos(); 26 | * Desc: Sends request for real mouse position, more expensive than getPos 27 | * Before: mouse is an initialized macmouse 28 | * After: pos holds x and y numbers representing the system mouse position 29 | */ 30 | var getRealPos = function() { 31 | var pos = $.NSEvent("mouseLocation"); 32 | return { x: pos.x, y: pos.y }; 33 | } 34 | 35 | /** 36 | * Usage: var pos = mouse.getPos(); 37 | * Desc: Returns mouse position currently stored in the mouse module 38 | * Before: mouse is an initialized macmouse 39 | * After: pos holds x and y numbers representing the system mouse position currently stored in the 40 | * mouse module 41 | */ 42 | var getPos = function() { 43 | return { x: ptX, y: ptY }; 44 | } 45 | 46 | // simple private helper function 47 | var setPos = function(x, y) { 48 | ptX = x; 49 | ptY = y; 50 | } 51 | 52 | /** 53 | * Usage: mouse.Place(); 54 | * Desc: Sends mouse event message to place the system mouse at a specific position 55 | * Before: mouse is an initialized macmouse, x and y are numbers representing a specific position 56 | * After: mouse event has been sent to move the system mouse to position defined by x and y 57 | */ 58 | var Place = function(x, y) { 59 | setPos(x, y); 60 | var moveEvent = $.CGEventCreateMouseEvent(null, $.kCGEventMouseMoved, $.CGPointMake(x, y), $.kCGMouseButtonLeft); 61 | $.CGEventPost($.kCGHIDEventTap, moveEvent); 62 | } 63 | 64 | /** 65 | * Usage: mouse.DragPlace(x, y); 66 | * Desc: Sends mouse event message to place the system mouse at a specific position while in a 67 | * dragging state 68 | * Before: mouse is an initialized macmouse, x and y are numbers representing a specific position, the 69 | * system mouse currently has (or thinks it has) the left mouse button pressed 70 | * After: mouse event has been sent to move the system mouse to position defined by x and y with left 71 | * mouse button pressed 72 | */ 73 | var DragPlace = function(x, y) { 74 | setPos(x, y); 75 | var moveEvent = $.CGEventCreateMouseEvent(null, $.kCGEventLeftMouseDragged, $.CGPointMake(x, y), $.kCGMouseButtonLeft); 76 | $.CGEventPost($.kCGHIDEventTap, moveEvent); 77 | } 78 | 79 | /** 80 | * Usage: mouse.Move(dx, dy); 81 | * Desc: Sends mouse event message to move the system mouse (from current stored position in the mouse 82 | * module) by a vector defined by dx and dy 83 | * Before: mouse is an initialized macmouse, dx and dy are numbers representing our moving vector 84 | * After: mouse event has been sent to move the system mouse by a vector defined by the numbers dx and dy 85 | */ 86 | var Move = function(dx, dy) { 87 | ptX += dx; 88 | ptY += dy; 89 | var moveEvent = $.CGEventCreateMouseEvent(null, $.kCGEventMouseMoved, $.CGPointMake(ptX, ptY), $.kCGMouseButtonLeft); 90 | $.CGEventPost($.kCGHIDEventTap, moveEvent); 91 | } 92 | 93 | /** 94 | * Usage: mouse.DragMove(dx, dy); 95 | * Desc: Sends mouse event message to move the system mouse (from current stored position in the mouse 96 | * module) by a vector defined by dx and dy while in a dragging state 97 | * Before: mouse is an initialized macmouse, dx and dy are numbers representing our moving vector, the 98 | * system mouse currently has (or thinks it has) the left mouse button pressed 99 | * After: mouse event has been sent to move the system mouse by a vector defined by the numbers dx and dy 100 | * with left mouse button pressed 101 | */ 102 | var DragMove = function(dx, dy) { 103 | ptX += dx; 104 | ptY += dy; 105 | var moveEvent = $.CGEventCreateMouseEvent(null, $.kCGEventLeftMouseDragged, $.CGPointMake(ptX, ptY), $.kCGMouseButtonLeft); 106 | $.CGEventPost($.kCGHIDEventTap, moveEvent); 107 | } 108 | 109 | /** 110 | * Usage: mouse.LeftButtonPress(); 111 | * Desc: Sends mouse event message to press and hold down the left button of the system mouse 112 | * Before: mouse is an initialized macmouse 113 | * After: mouse event has been sent to press and hold the left button on the system mouse 114 | */ 115 | var LeftButtonPress = function() { 116 | var clickDown = $.CGEventCreateMouseEvent(null, $.kCGEventLeftMouseDown, $.CGPointMake(ptX, ptY), $.kCGMouseButtonLeft); 117 | $.CGEventPost($.kCGHIDEventTap, clickDown); 118 | } 119 | 120 | /** 121 | * Usage: mouse.LeftButtonRelease(); 122 | * Desc: Sends mouse event message to release a pressed left button of the system mouse 123 | * Before: mouse is an initialized macmouse 124 | * After: mouse event has been sent to release a pressed left button on the system mouse 125 | */ 126 | var LeftButtonRelease = function() { 127 | var clickUp = $.CGEventCreateMouseEvent(null, $.kCGEventLeftMouseUp, $.CGPointMake(ptX, ptY), $.kCGMouseButtonLeft); 128 | $.CGEventPost($.kCGHIDEventTap, clickUp); 129 | } 130 | 131 | /** 132 | * Usage: mouse.Click(); 133 | * Desc: Sends mouse event message to press and release left button of the system mouse 134 | * Before: mouse is an initialized macmouse 135 | * After: mouse event has been sent to press and release left button on the system mouse 136 | */ 137 | var Click = function() { 138 | LeftButtonPress(); 139 | LeftButtonRelease(); 140 | } 141 | 142 | /** 143 | * Usage: mouse.RightButtonPress(); 144 | * Desc: Sends mouse event message to press and hold down the right button of the system mouse 145 | * Before: mouse is an initialized macmouse 146 | * After: mouse event has been sent to press and hold the right button on the system mouse 147 | */ 148 | var RightButtonPress = function() { 149 | var clickDown = $.CGEventCreateMouseEvent(null, $.kCGEventRightMouseDown, $.CGPointMake(ptX, ptY), $.kCGEventRightMouseDown); 150 | $.CGEventPost($.kCGHIDEventTap, clickDown); 151 | } 152 | 153 | /** 154 | * Usage: mouse.RightButtonRelease(); 155 | * Desc: Sends mouse event message to release a pressed right button of the system mouse 156 | * Before: mouse is an initialized macmouse 157 | * After: mouse event has been sent to release a pressed right button on the system mouse 158 | */ 159 | var RightButtonRelease = function() { 160 | var clickUp = $.CGEventCreateMouseEvent(null, $.kCGEventRightMouseUp, $.CGPointMake(ptX, ptY), $.kCGEventRightMouseDown); 161 | $.CGEventPost($.kCGHIDEventTap, clickUp); 162 | } 163 | 164 | /** 165 | * Usage: mouse.RightClick(); 166 | * Desc: Sends mouse event message to press and release right button of the system mouse 167 | * Before: mouse is an initialized macmouse 168 | * After: mouse event has been sent to press and release right button on the system mouse 169 | */ 170 | var RightClick = function() { 171 | RightButtonPress(); 172 | RightButtonRelease(); 173 | } 174 | 175 | /** 176 | * Usage: mouse.DoubleClick(); 177 | * Desc: Sends mouse event message to double click the system mouse 178 | * Before: mouse is an initialized macmouse 179 | * After: mouse event has been sent to double click the system mouse 180 | */ 181 | var DoubleClick = function() { 182 | var evt = $.CGEventCreateMouseEvent(null, $.kCGEventLeftMouseDown, $.CGPointMake(ptX, ptY), $.kCGMouseButtonLeft); 183 | $.CGEventSetIntegerValueField(evt, $.kCGMouseEventClickState, 2); 184 | $.CGEventPost($.kCGHIDEventTap, evt); 185 | $.CGEventSetType(evt, $.kCGEventLeftMouseUp); 186 | $.CGEventPost($.kCGHIDEventTap, evt); 187 | $.CGEventSetType(evt, $.kCGEventLeftMouseDown); 188 | $.CGEventPost($.kCGHIDEventTap, evt); 189 | $.CGEventSetType(evt, $.kCGEventLeftMouseUp); 190 | $.CGEventPost($.kCGHIDEventTap, evt); 191 | } 192 | 193 | /** 194 | * Usage: mouse.Scroll(vertical, horizontal); 195 | * Desc: Sends mouse scroll event message 196 | * Before: mouse is an initialized macmouse, vertical and horizontal 197 | * are 'small signed integer values, typically in a range from -10 to +10', 198 | * in reality they can be any integer from -32768 to 32767, 199 | * if horizontal isn't provided it defaults to 0 200 | * After: scroll event has been sent to scroll by a vector defined by the 201 | * vertical and horizontal integers 202 | */ 203 | var Scroll = function(vertical, horizontal) { 204 | if (typeof horizontal === 'undefined') horizontal = 0; 205 | var scrollEvent = $.CGEventCreateScrollWheelEvent(null, $.kCGScrollEventUnitLine, 1, vertical, horizontal); 206 | $.CGEventPost($.kCGHIDEventTap, scrollEvent); 207 | } 208 | 209 | /** 210 | * Usage: mouse.quit(); 211 | * Desc: Does garbage collection some on objective c stuff, be a good lad and call this when 212 | * you're done using the macmouse module 213 | * Before: mouse is an initialized macmouse 214 | * After: mouse is an uninitialized macmouse 215 | */ 216 | var quit = function() { 217 | pool('drain'); 218 | } 219 | 220 | module.exports = { 221 | init: init, 222 | getRealPos: getRealPos, 223 | getPos: getPos, 224 | Place: Place, 225 | DragPlace: DragPlace, 226 | Move: Move, 227 | DragMove: DragMove, 228 | LeftButtonPress: LeftButtonPress, 229 | LeftButtonRelease: LeftButtonRelease, 230 | Click: Click, 231 | RightButtonPress: RightButtonPress, 232 | RightButtonRelease: RightButtonRelease, 233 | RightClick: RightClick, 234 | DoubleClick: DoubleClick, 235 | Scroll: Scroll, 236 | quit: quit 237 | } 238 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "macmouse", 3 | "version": "1.2.1", 4 | "description": "Enables you to create virtual mouse input on Mac OS X using Cocoa.", 5 | "author": "Sveinn Floki Gudmundsson ", 6 | "contributors": [{ 7 | "name": "Sveinn Floki Gudmundsson", 8 | "email": "svefgud@gmail.com" 9 | }], 10 | "repository" : 11 | { "type" : "git", 12 | "url" : "https://github.com/Loknar/node-macmouse.git" 13 | }, 14 | "main": "macmouse.js", 15 | "keywords": [ 16 | "native mouse", 17 | "mac os x" 18 | ], 19 | "bugs": { 20 | "url": "https://github.com/Loknar/node-macmouse/issues" 21 | }, 22 | "engines": { 23 | "node": "*" 24 | }, 25 | "dependencies": { 26 | "nodobjc": "2.1.0" 27 | }, 28 | "license": "MIT" 29 | } 30 | --------------------------------------------------------------------------------