NodeJS native mouse and keyboard bindings [closed]

大憨熊 提交于 2019-11-29 01:33:14

问题


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.


回答1:


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




回答2:


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();



回答3:


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);



回答4:


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();


来源:https://stackoverflow.com/questions/15231713/nodejs-native-mouse-and-keyboard-bindings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!