Atom Electron capture all keyboard events even when app is unfocused

旧城冷巷雨未停 提交于 2019-12-05 09:31:28

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.

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 modules like iohook

const ioHook = require('iohook');

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

ioHook.start();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!