我正在尝试使用节点js创建一个程序来捕获按键和鼠标移动。不在网络浏览器上。这是我个人项目的一种键盘记录器类型。我尝试过robotjs,但需要很多依赖才能运行。有没有简单的方法可以做到这一点。 提前致谢
答案 0 :(得分:5)
看起来你需要全局密钥钩 尝试使用iohook模块
'use strict';
const ioHook = require('iohook');
ioHook.on("mousemove", event => {
console.log(event);
// result: {type: 'mousemove',x: 700,y: 400}
});
ioHook.on("keypress", event => {
console.log(event);
// result: {keychar: 'f', keycode: 19, rawcode: 15, type: 'keypress'}
});
//Register and stark hook
ioHook.start();
它是跨平台本机模块,适用于Windows,Linux,MacOS
答案 1 :(得分:0)
您是否尝试过使用按键模块? https://github.com/TooTallNate/keypress
KEY的回购示例:
var keypress = require('keypress');
// use decoration to enable stdin to start sending ya events
keypress(process.stdin);
// listen for the "keypress" event
process.stdin.on('keypress', function (ch, key) {
console.log('got "keypress"', key);
if (key && key.ctrl && key.name == 'c') {
process.stdin.pause();
}
});
process.stdin.setRawMode(true);
process.stdin.resume();
鼠标回购的示例: var keypress = require('keypress');
// make `process.stdin` begin emitting "mousepress" (and "keypress") events
keypress(process.stdin);
// you must enable the mouse events before they will begin firing
keypress.enableMouse(process.stdout);
process.stdin.on('mousepress', function (info) {
console.log('got "mousepress" event at %d x %d', info.x, info.y);
});
process.on('exit', function () {
// disable mouse on exit, so that the state
// is back to normal for the terminal
keypress.disableMouse(process.stdout);
});