11

I've been looking for a native nodejs module that supports mouse and keyboard listening and execution

i found this.. https://npmjs.org/package/mouse but the source code looks like it only supports the browsers.

bmatusiak
  • 151
  • 1
  • 1
  • 7
  • You're looking to capture mouse and keypress events in the terminal? – Pero P. Mar 05 '13 at 19:13
  • well if your saying listening for mouse movement in nodejs and then doing `console.log(mouse.x,mouse.y);` then yes – bmatusiak Mar 05 '13 at 19:29
  • Did you ever find an answer to this? – Shane Gadsby Jun 22 '13 at 06:40
  • 1
    What environment are you trying to capture the input events from? To get raw key events in node use http://nodejs.org/api/tty.html#tty_rs_setrawmode_mode. But everything else is highly platform dependent. Is this for linux, osx, windows, terminal, X11, Cocoa, etc? – Tim Caswell Oct 24 '13 at 20:40

4 Answers4

12

I've been working on a module for sending mouse and keyboard events, RobotJS.

Example code:

var robot = require("robotjs");

//Get the mouse position, retuns an object with x and y. 
var mouse=robot.getMousePos();
console.log("Mouse is at x:" + mouse.x + " y:" + mouse.y);

//Move the mouse down by 100 pixels.
robot.moveMouse(mouse.x,mouse.y+100);

//Left click!
robot.mouseClick();

Then for listening I use nw.js:

https://github.com/nwjs/nw.js/wiki/Shortcut

Jason Stallings
  • 1,443
  • 20
  • 17
4

You can wrap the Robot Class provided by Java for a cross platform solution using the node-java module.

Working example:

var java = require('java');

var Robot = java.import('java.awt.Robot');
var robot = new Robot();

robot.mouseMoveSync(0, 0);
Jay
  • 18,959
  • 11
  • 53
  • 72
  • Did you ever test this to get it to work? On OSX Just tried it, it launches a Java Icon in the dock, but then does nothing, any ideas? – owenmelbz Feb 08 '14 at 12:59
  • I tested it on Arch Linux using java version 1.7 (Oracle Java), node.js version 0.10.25, and the node-java module version 0.3.0. – Jay Feb 08 '14 at 19:04
  • @OwenMelbourne I don't work on the node-java project. You can try writing a simple Java program yourself that does this same thing and see if that works. If it does, you can [open a new issue](https://github.com/joeferner/node-java/issues) on their project page with the code you used (both Java & Node.js code), operating system, version numbers, etc. and see if they can help you further. – Jay Feb 08 '14 at 19:10
  • If the above answer didn't work for you on OSX and you're just looking for simple mouse execution (no mouse listening) you might like this minimalistic wrapper https://github.com/Loknar/node-macmouse. It uses the NodObjC module to hook into the Cocoa framework and send mouse commands to the system. – Loknar Aug 28 '14 at 14:43
4

Try iohook module.
It support Windows/Linux/MacOS

'use strict';
const ioHook = require('iohook');

ioHook.on("mousemove", event => {
   console.log(event);
   /* You get object like this
   {
      type: 'mousemove',
      x: 700,
      y: 400
    }
   */
});
// For keyboard hook
ioHook.on("keydown", event => { .... });
ioHook.on("keyup", event => { .... });

//Register and start hook 
ioHook.start();
Joviallix
  • 1,069
  • 10
  • 15
1

Take a look at https://github.com/Loknar/node-macmouse

$ npm install macmouse

example.js

var mouse = require('macmouse');

mouse.init();

var ptX = 800;
var ptY = 600;

var doThings = function() {
    mouse.Place(ptX, ptY);
    setTimeout(pressAndHold, 250);
}

var pressAndHold = function() {
    mouse.LeftButtonPress();
    setTimeout(doDragStuff, 250);
}

var doDragStuff = function() {
    ptX += 2;
    ptY += 2;
    mouse.DragPlace(ptX, ptY);
    setTimeout(doDragStuff, 250);
}

doThings();

mouse.quit();
Community
  • 1
  • 1
John Murch
  • 199
  • 1
  • 1
  • 5