【问题标题】:nodejs keydown/keyup eventsnodejs keydown/keyup 事件
【发布时间】:2014-06-12 14:34:38
【问题描述】:

我有兴趣看看是否可以将功能绑定到用户按下/释放键盘上的键。

到目前为止,我已经能够使用keypress 模块和process.stdin 的原始模式获取按键事件:

var keypress = require('keypress');
keypress(process.stdin);
process.stdin.on('keypress', function(ch, key) {
    console.log('got "keypress"', key);
    if (key && key.ctrl && key.name == 'c') {
        process.exit();
    }
});
process.stdin.setRawMode(true);
process.stdin.resume();

是否可以在终端中捕获键盘按下释放事件?

值得注意的是,我对任何浏览器实现都不感兴趣;我正在寻找在终端的 NodeJS 中运行的东西。

谢谢大家。

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    所以我想出了一个解决 stdin 限制的方法来让它工作。

    基本上,我使用 SDL 库(连同它的 node bindings)在后台运行一个程序,该程序在键盘上侦听输入。

    为此:

    • 确保您正在运行 Node 版本 ~0.10。 (显然节点上的 C 绑定在 0.11 中的工作方式略有不同?)
    • 通过自制软件(在 Mac 上)安装 sdlsdl_ttfsdl_image
    • npm install --save https://github.com/creationix/node-sdl/tarball/master

    然后是这样的:

    var sdl = require('sdl');
    
    sdl.init(sdl.INIT.VIDEO);
    
    while (true) {
        var e;
        while (e = sdl.pollEvent()) {
            console.log(e);
        }
    }
    

    SDL_PollEvent 要求使用SDL_INIT_VIDEO 初始化 SDL,这在上面的脚本中(在 Mac 上)会在 Dock 中启动一个单独的应用程序,该应用程序不绘制任何内容,但需要集中注意力以接受输入。

    虽然 ANSI 终端根本不支持 keyup 事件在技术上是正确的,但这种解决方法完全可以在 Node 中抓取 keyup 事件(尽管需要一些基于 GUI 的系统才能运行,即很可能无法通过 ssh 工作)。

    【讨论】:

      【解决方案2】:

      在 Linux 桌面上,您可以将“xev”命令的输出通过管道传输到您的模块中,然后解析流以发出您自己的“keyup”和“keydown”事件。可能不如 SDL 便携,但更直接。有一个module 可以做到这一点。 (免责声明:我写的)。

      const xevEmitter = require('xev-emitter')(process.stdin)
      xevEmitter.on('KeyPress', (key) => {
          console.log(key, 'was pressed')
      })
      
      xevEmitter.on('KeyRelease', (key) => {
          console.log(key, 'was released')
      })
      

      执行:

      $ xev | node example.js
      h was pressed
      h was released
      

      【讨论】:

        【解决方案3】:

        这是一个很好的工具: https://www.npmjs.com/package/iohook

        看示例代码:

        npm install iohook
        
        
        
        const ioHook = require('iohook');
        
        /* In next example we register CTRL+F7 shortcut (in MacOS, for other OS, keycodes can be some different). */
        
        const id = ioHook.registerShortcut([29, 65], (keys) => {
          console.log('Shortcut called with keys:', keys)
        });
        
        ioHook.on("keydown", event => {
           console.log(event);
           /* You get object like this
           {
              shiftKey: true,
              altKey: true,
              ctrlKey: false,
              metaKey: false
              keycode: 46,
              rawcode: 8,
              type: 'keydown'
            }
           */
        });
        
        //register and start hook
        ioHook.start();
        
        // Alternatively, pass true to start in DEBUG mode.
        ioHook.start(true);
        

        包ioHook doc的使用链接 https://wilix-team.github.io/iohook/usage.html

        【讨论】:

          【解决方案4】:

          只有keypress,没有keydown/keyup。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-04-27
            • 1970-01-01
            • 2012-02-08
            • 1970-01-01
            • 1970-01-01
            • 2017-10-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多