nodejs keydown/keyup events

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

提交回复
热议问题