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?
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();
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With