nodejs keydown/keyup events

后端 未结 4 999
忘了有多久
忘了有多久 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: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
    

提交回复
热议问题