节点js捕获键盘按下和鼠标移动(不在Web浏览器上)

问题描述 投票:6回答:2

我正在尝试使用节点js创建一个程序来捕获按键和鼠标移动。不在网络浏览器上。这是我个人项目的一种键盘记录器类型。我尝试过robotjs,但需要很多依赖才能运行。有没有简单的方法可以做到这一点。提前致谢

javascript node.js keyboard keypress
2个回答
5
投票

看起来你需要全局密钥钩子。 尝试使用iohook模块

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

ioHook.on("mousemove", event => {
  console.log(event);
  // result: {type: 'mousemove',x: 700,y: 400}
});
ioHook.on("keydown", event => {
  console.log(event);
  // result: {keychar: 'f', keycode: 19, rawcode: 15, type: 'keypress'}
});
//Register and stark hook 
ioHook.start();

它是跨平台本机模块,适用于Windows,Linux,MacOS


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

来自Mouse的repo的例子: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);
});
© www.soinside.com 2019 - 2024. All rights reserved.