javascript - 无法让 globalShortcut 使用 Node/Electron javascript 应用程序中的发送函数将命令注册到 index.js

标签 javascript node.js electron

由于某种原因,我的代码编译没有错误,但是,我的消息“Helloworld”没有在控制台中正确显示。但是,当我按下绑定(bind)的组合键时,会显示我的测试消息。下面是我的一组代码,index.js 和 main.js

这是为 Node/Electron 编写的。 我的 main.js 文件:

   //main.js

   //requirements
   const electron = require('electron');
   const app = require('electron').app;
   const BrowserWindow = require('electron').BrowserWindow;
   const remote = require('electron').remote;
   const ipc = require('electron').ipcMain;
   const globalShortcut = require('electron').globalShortcut;

   var mainWindow = null;

   //create app, instantiate window
   app.on('ready', function() {
       mainWindow = new BrowserWindow({
           frame: false,
           height: 700,
           resizable: false,
           width: 368
       });

       mainWindow.loadURL(`file://${__dirname}/app/index.html`);

       //this is the icp bind
       globalShortcut.register('ctrl+shift+1', function(){
           console.log("test")
           mainWindow.webContents.send("testBindicp" ,"HelloWorld");
       });

       //this is the remote bind
       globalShortcut.register('ctrl+shift+2', function(){
           console.log("test")
           mainWindow.webContents.send("testBindicp" ,"HelloWorld");
       });
   });

   //close the app
   ipc.on('close-main-window', function () {
       app.quit();
   });

下面是我的整个 index.js:

   //index.js
   const globalShortcut = require('electron').globalShortcut;
   const remote = require('electron').remote;
   const ipc = require('electron').ipcRenderer;

   //testing remote render from remote bind
   remote.require('./main.js');
   remote.on('testBindRemote', function(event){
       console.log(event + " - test - from remote index.js");
   });

   //testing icpRenderer from icp bind
   ipc.on('testBindicp', function (event) {
       console.log(event + " - test - from icpRenderer on index.js")
   });

  //close the app
   var closeEl = document.querySelector('.close');
   closeEl.addEventListener('click', function() {
       ipc.send('close-main-window');
   });

我遇到的问题是当我按下键盘绑定(bind)时,只有来自 main.js 文件的控制台日志被发送到控制台。关闭命令仍在渲染窗口中工作,但 index.js 窗口中的任何其他命令都没有绑定(bind)到正确的 main.js 元素。

如果我做错了什么,请告诉我实现这些方法的正确方法,因为 remote 和 icp 结构似乎让我感到困惑。

谢谢。

最佳答案

需要在index.js文件的监听进程ipc.on中传入另外一个参数,如下所示:

ipc.on(<channel name>, function (event, arg) {
       console.log(arg + " - test - from icpRenderer on index.js");
   });

欲了解更多信息,请访问 webContents API docs

关于javascript - 无法让 globalShortcut 使用 Node/Electron javascript 应用程序中的发送函数将命令注册到 index.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39612392/

相关文章:

javascript - 使用 pdfmake 库时文本离开页面

javascript - Facebook 发送请求 未发送请求

javascript - 将音频isPlaying状态设置为当前音频唯一

node.js - 如何对外键进行条件聚合查找

node.js - 是否有一个 npm 命令可以在不安装的情况下向 package.json 添加依赖项?

javascript - 使用 Node js 在 TypeScript 中获取操作系统环境

javascript - v-bind.sync 不将对象作为 Prop 传递

javascript - 当保存的文件被覆盖/替换时, Electron 窗口会重新加载

node.js - 没有代码签名就无法构建 Electron 应用程序

vue.js - 在生产中使用 Electron 加载nanosql数据库数据的问题