我试图添加键盘快捷键,专门为电子(web)构建一个反应-本机-web应用程序。
我确实有一些快捷键在index.electron.js
中已经被定义为加速器,但是我希望这些新的快捷键不在菜单中使用,它们需要响应动作/还原器来执行。
我尝试将window.onkeydown = (e) => {
添加到App.web.js::componentDidMount
中,但是这只在浏览器中运行应用程序时才有效。当我构建应用程序并以电子方式运行它(它运行的是.web
版本的文件)时,我会因为没有定义window
而崩溃。
我很难理解如何将键盘快捷键/事件侦听器(触发特定于反应的代码)添加到一个使用React构建的电子应用程序中。
提前谢谢你
发布于 2020-02-24 19:41:24
您应该使用电子API globalShortcut
和IPC将事件发送到网页。
主工艺代码
const { globalShortcut } = require('electron')
// this will be triggered when the accelerator is pressed
// even if the app is in the background
const ret = globalShortcut.register("Your_Accelerator", () => {
yourWindow.webContents.send("shortcut", "shortcut has been pressed")
});
if (!ret) {
console.log("registration failed");
} else {
console.log("registration succeeded");
}
渲染程序处理代码
const { ipcRenderer } = require("electron");
ipcRenderer.on("shortcut", (event, message) => {
console.log(message) // Prints "shortcut has been pressed"
})
发布于 2020-02-24 19:21:31
我不经常使用react本机,所以我不确定是否有可能仅用框架来完成这个任务。假设您使用的是npm
,您只需下载一个可以支持此功能的包即可。反应热键听起来好像可以做你需要的快捷方式。
https://stackoverflow.com/questions/60386041
复制