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

Pra*_*nce 6 javascript keyboard keypress node.js

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

Alo*_*try 6

看起来您需要全局密钥挂钩。
尝试使用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();
Run Code Online (Sandbox Code Playgroud)

它是跨平台的本机模块,可在Windows,Linux,MacOS上运行

  • 无法在 Windows 上运行这个 (2认同)

小智 3

您是否尝试过使用按键模块?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();
Run Code Online (Sandbox Code Playgroud)

鼠标存储库中的示例: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);
});
Run Code Online (Sandbox Code Playgroud)

  • 是的,我尝试过,但这不是我想要的,我只是想要类似于真正的键盘记录器的东西,无论我在哪里键入并单击,它都会实际跟踪我的按键按下/释放 (2认同)
  • “keypress”模块也有点坏了。 (2认同)