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

Node js capture keyboard press and mouse movement (not on Web Browser)

本文关键字:不在 移动 Web 浏览器 鼠标 js 键盘 Node      更新时间:2023-09-26

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

看起来你需要全局键钩。
尝试使用 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

您是否尝试过使用按键模块? 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 按键 = require('按键');

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