reactjs - Electron React Native Web 键盘快捷键

标签 reactjs react-native electron keyboard-shortcuts react-native-web

我正在尝试专门为react-native-web应用程序的 Electron (网络)构建添加键盘快捷键。

我确实已经在 index.electron.js 中定义了一些快捷方式作为加速器,但我希望这些新快捷方式不在菜单中,并且它们需要 react 操作/ reducer 才能执行。

我尝试将 window.onkeydown = (e) => { 添加到 App.web.js::componentDidMount 中,但这仅在运行应用程序时有效浏览器。当我构建应用程序并在 Electron 中运行它(运行 .web 版本的文件)时,我遇到了关于未定义 window 的崩溃。

我对如何向使用 React-Native for web 构建的 Electron 应用程序添加键盘快捷键/事件监听器(触发特定于 react 的代码)感到困惑。

提前谢谢

最佳答案

您应该使用 Electron API globalShortcutIPC将事件发送到网页。

主要流程代码

  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"
  })

关于reactjs - Electron React Native Web 键盘快捷键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60386041/

相关文章:

javascript - React 解析具有多个单词标识符的 JSON 数据

javascript - 原子 Electron : Render Canvas in Another Window

javascript - 切换按钮来控制按钮

reactjs - 数组中的项目已删除但 UI 未在 React Native 中更新

node.js - Electron chrome和node的setInterval实现区别

javascript - 为 NightmareJS 指定 SOCKS 代理?

javascript - CKEditor 如何以允许 React 识别的方式与 React.js 一起使用?

reactjs - '(e : ChangeEvent<HTMLInputElement>) => void' is not assignable to type '((event: ChangeEvent<HTMLInputElement>) => void)

javascript - React Router 检查实际路径

android - 有什么方法可以在React Native中预取或缓存音频吗?