Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atom Electron capture all keyboard events even when app is unfocused

I would like to know if there is a way to make an application with Atom Electron that runs and gets my keyboard events when the user is/isn't focused on my app.

For example if he is on Chrome and writes something, my app will store all the keys that he pressed. I searched a little but didn't find something that solves my problem.

like image 208
Nistor Cristian Avatar asked Sep 08 '16 13:09

Nistor Cristian


2 Answers

The closest thing there is to what you're looking for is global shortcuts: https://github.com/electron/electron/blob/master/docs/api/global-shortcut.md. While you don't have anything in core Electron to support capturing all keyboard events out of the box, luckily node.js is pretty extensible with native node addons.

like image 75
ccnokes Avatar answered Nov 12 '22 14:11

ccnokes


For global shortcuts you can use Electron Keyboard-Shortcuts module

const {app, globalShortcut} = require('electron')

app.on('ready', () => {
  globalShortcut.register('CommandOrControl+X', () => {
    console.log('CommandOrControl+X is pressed')
  })
})

But this module support only shortcuts.
If you need any key listening/hooking you should use another module like iohook

const ioHook = require('iohook');

ioHook.on("keyup", event => {
   console.log(event); // {keychar: 'f', keycode: 19, rawcode: 15, type: 'keup'}
});

ioHook.start();
like image 31
Aloyan Dmitry Avatar answered Nov 12 '22 12:11

Aloyan Dmitry