Atom Electron 捕获所有键盘事件,即使应用程序未聚焦
Atom Electron capture all keyboard events even when app is unfocused
我想知道是否有一种方法可以使用 Atom Electron 创建一个应用程序,当用户 is/isn 没有关注我的应用程序时运行并获取我的键盘事件。
例如,如果他在 Chrome 上并写了一些东西,我的应用程序将存储他按下的所有键。我搜索了一下,但没有找到解决我问题的方法。
最接近您要查找的内容是全局快捷方式: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。
对于全局快捷方式,您可以使用 Electron Keyboard-Shortcuts module
const {app, globalShortcut} = require('electron')
app.on('ready', () => {
globalShortcut.register('CommandOrControl+X', () => {
console.log('CommandOrControl+X is pressed')
})
})
但此模块仅支持快捷方式。
如果你需要任何键 listening/hooking 你应该使用另一个模块,比如 iohook
const ioHook = require('iohook');
ioHook.on("keyup", event => {
console.log(event); // {keychar: 'f', keycode: 19, rawcode: 15, type: 'keup'}
});
ioHook.start();
我想知道是否有一种方法可以使用 Atom Electron 创建一个应用程序,当用户 is/isn 没有关注我的应用程序时运行并获取我的键盘事件。
例如,如果他在 Chrome 上并写了一些东西,我的应用程序将存储他按下的所有键。我搜索了一下,但没有找到解决我问题的方法。
最接近您要查找的内容是全局快捷方式: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。
对于全局快捷方式,您可以使用 Electron Keyboard-Shortcuts module
const {app, globalShortcut} = require('electron')
app.on('ready', () => {
globalShortcut.register('CommandOrControl+X', () => {
console.log('CommandOrControl+X is pressed')
})
})
但此模块仅支持快捷方式。
如果你需要任何键 listening/hooking 你应该使用另一个模块,比如 iohook
const ioHook = require('iohook');
ioHook.on("keyup", event => {
console.log(event); // {keychar: 'f', keycode: 19, rawcode: 15, type: 'keup'}
});
ioHook.start();