看到这个问题后,我对你的主题做了一个小的研究。首先想到的是电子是否允许监听关键事件,但据此thread 电子开发人员正在阻止电子成为 键盘记录器。所以我有以下方法来解决这个问题。我不知道这些是否最适合这种情况,但这是我认为可以做到的方式。这基本上是围绕电子商店构建的,它可以用来保存用户定义的组合键。所以应用程序可以从商店中检索定义的组合键(如果没有配置组合,它使用架构上提供的默认组合键)并使用它注册一个globalshortcut。我已经提供了如何实现它的步骤
- 使用electron-store 安装和配置默认键值。如下所示
main.js
const Store = require('electron-store');
const schema = {
defaultKeyCombination: {
type: 'string',
default: 'CommandOrControl+Y'
}
}
const store = new Store({schema});
- 导入此
defaultKeyCombination,您可以在应用准备就绪时注册global-shortcut。 (当应用程序被销毁时,不要忘记删除全局快捷方式)
app.on('ready', () => {
// Register a defaultKeyCombination shortcut listener.
globalShortcut.register(store.get('defaultKeyCombination'), () => {
// Do stuff when Y and either Command/Control is pressed.
})
})
-
通过单击菜单(菜单栏>选项>配置)创建并打开另一个浏览器窗口,并让用户使用可用的modifiers和key codes在输入框中创建/输入accelerator修饰符(更好在输入框下方的新窗口中显示这些);
例如:用户可以在浏览器中输入MODIFIER+KEY_CODE如CmdOrCtrl+A。
-
一旦用户按下submit按钮,将使用IPCRenderer输入的组合键发送到主进程,并通过接收到的值设置存储defaultKeyCombination值。
-
触发 IPC 向用户发送回复说“请重新启动应用程序”并在警报或任何情况下显示它。
渲染进程
let args = "CmdOrCtrl+A"
ipcRenderer.send('set::keycombine',args)
ipcRenderer.on('done::keycombine', (event, arg) => {
// display message to restart the app
})
主进程
ipcMain.on('set::keycombine', (event, arg) => {
console.log(arg) // prints "keycombine"
//setting the new value
store.set('defaultKeyCombination', arg)
// sending reply to renderer work is done
event.reply('done::keycombine')
})
应用重新启动后,商店将加载新配置的组合键并使用它注册快捷事件。
这是我在进行这项小型研究时想到的。在这里,我找到了一个名为 iohook 的关键事件侦听器,但这仅适用于电子 2.XX 。在上面的过程中可能存在错误和流程问题,我只是发布了一些代码来了解一下。
编辑 1:
这是我的样品。在我的 index.html 上,我定义了一个按钮来调用 set() 函数。您可以集成输入框,以便输入命令。使用存储设置键后,除非用户更改它,否则它将始终使用此新键值加载。你可以阅读更多关于electron-store from here的信息希望这会给你一个想法:)
Main.js
const {app, BrowserWindow, ipcMain } = require('electron')
const Store = require('electron-store');
const schema = {
defaultKeyCombination: {
type: 'string',
default: 'CommandOrControl+Y'
}
}
const store = new Store({schema});
console.log(store.get("defaultKeyCombination"))
function createWindow () {
const window = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
window.loadFile('./index.html')
// window.loadURL("https://www.zap.co.il")
window.webContents.openDevTools()
}
ipcMain.on('set::keycombine', (event, arg) => {
console.log(arg) // prints "keycombine"
//setting the new value
store.set('defaultKeyCombination', arg)
// sending reply to renderer work is done with new key
event.reply('done::keycombine', store.get('defaultKeyCombination'))
})
app.wheReady().then(createWindow)
//app.on('ready', createWindow)
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
Renderer.js
const { ipcRenderer } = require('electron')
function set() {
console.log("clicked")
let args = "CmdOrCtrl+A"
ipcRenderer.send('set::keycombine',args)
}
ipcRenderer.on('done::keycombine', (event, arg) => {
console.log("DONEEEEEEEEEE", arg)
})