nodejs keydown/keyup events

后端 未结 4 1000
忘了有多久
忘了有多久 2021-01-13 03:58

I\'m interested in seeing if it\'s possible to bind functions to a user pressing/releasing a key on the keyboard.

So far, I\'ve been able to get key press events wit

相关标签:
4条回答
  • 2021-01-13 04:41

    That is a good tool: https://www.npmjs.com/package/iohook

    Look at the sample code:

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

    Link of usage of package ioHook doc https://wilix-team.github.io/iohook/usage.html

    0 讨论(0)
  • 2021-01-13 04:49

    So I figured a workaround the limitations of stdin to get this to work.

    Basically I use the SDL library (along with its node bindings) to run a program in the background that listens on the keyboard for input.

    To do this:

    • Ensure you are running Node version ~0.10. (Apparently the way C bindings on node work a little differently in 0.11?)
    • Install sdl, sdl_ttf, and sdl_image through homebrew (on a mac)
    • npm install --save https://github.com/creationix/node-sdl/tarball/master

    And then something along the lines of:

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

    SDL_PollEvent requires SDL be initialized with SDL_INIT_VIDEO, which in the script above (on a mac) starts a separate application in the dock that draws nothing, but needs to be focused to accept input.

    While it's technically true that ANSI terminals simply do not support keyup events, this workaround totally lets one grab keyup events in Node (albeit requiring some GUI-based system to function, i.e. most likely will not work over ssh).

    0 讨论(0)
  • 2021-01-13 04:54

    There is only keypress, no keydown/keyup.

    0 讨论(0)
  • 2021-01-13 04:55

    On a Linux desktop, you can pipe the output of the 'xev' command into your module, and then parse the stream to emit your own 'keyup' and 'keydown' events. Less portable than SDL perhaps, but more straightforward. There's a module to do this. (Disclaimer: I wrote it).

    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')
    })
    

    Executing:

    $ xev | node example.js
    h was pressed
    h was released
    
    0 讨论(0)
提交回复
热议问题