当您在不同的应用程序上时如何使事件侦听器工作
How to make an event listener work when you are on a different application
我正在使用 electronjs 创建一个带有事件侦听器的应用程序,其中一个功能是当按下键盘上的“a”键时,会发生一些事情。但是当我在不同的应用程序上时,这个监听器不起作用。
事件监听器的代码是:
document.addEventListener('keydown', (event) => {
//function
}
您需要注册一个globalShortcut
,记得在应用程序要退出时取消注册。
以下是您可以使用的键和修饰符的文档:https://www.electronjs.org/docs/api/accelerator
const { app, globalShortcut } = require('electron')
app.whenReady().then(() => {
// Register a 'A' shortcut listener.
const ret = globalShortcut.register('A', () => {
console.log('A is pressed')
})
if (!ret) {
console.log('registration failed')
}
// Check whether a shortcut is registered.
console.log(globalShortcut.isRegistered('A'))
})
app.on('will-quit', () => {
// Unregister a shortcut.
globalShortcut.unregister('A')
// Unregister all shortcuts.
globalShortcut.unregisterAll()
})
我正在使用 electronjs 创建一个带有事件侦听器的应用程序,其中一个功能是当按下键盘上的“a”键时,会发生一些事情。但是当我在不同的应用程序上时,这个监听器不起作用。
事件监听器的代码是:
document.addEventListener('keydown', (event) => {
//function
}
您需要注册一个globalShortcut
,记得在应用程序要退出时取消注册。
以下是您可以使用的键和修饰符的文档:https://www.electronjs.org/docs/api/accelerator
const { app, globalShortcut } = require('electron')
app.whenReady().then(() => {
// Register a 'A' shortcut listener.
const ret = globalShortcut.register('A', () => {
console.log('A is pressed')
})
if (!ret) {
console.log('registration failed')
}
// Check whether a shortcut is registered.
console.log(globalShortcut.isRegistered('A'))
})
app.on('will-quit', () => {
// Unregister a shortcut.
globalShortcut.unregister('A')
// Unregister all shortcuts.
globalShortcut.unregisterAll()
})