Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs listen for global hotkey press

Tags:

node.js

I'm not using Node as a server; I'm using it for CLI apps. This one though needs to run in the background and listen for global key press events (without having focus).

I don't see any obvious way of doing this in Node. What are my options?

like image 293
Farzher Avatar asked Sep 18 '13 21:09

Farzher


3 Answers

Looks like you need global hook for all keyboard events.
You can try iohook module. Using is pretty simple:

const ioHook = require('iohook');
ioHook.on("keypress", event => {
  console.log(event);
  // {keychar: 'f', keycode: 19, rawcode: 15, type: 'keypress'}
});
ioHook.start();
like image 131
Aloyan Dmitry Avatar answered Nov 14 '22 09:11

Aloyan Dmitry


I just do it with iohook. You could do something like this...

const ioHook = require('./node_modules/iohook-master/index.js');

ioHook.on("keyup",function(keyPress){
    if(keyPress.keycode == CTRLIZQ){
        //do something
    }
});

ioHook.start();
like image 3
Athos Oc Avatar answered Nov 14 '22 08:11

Athos Oc


Seems like a combination of daemon and keypress could do you want. I've only ever used keypress in a node script, not a daemon, so I have no idea if it will work the same way. But it might! At the very worst, you'll discover one solution that doesn't solve your problem.

like image 2
tandrewnichols Avatar answered Nov 14 '22 07:11

tandrewnichols