无法得到globalShortcut注册命令index.js使用发送函数在节点/电子JavaScript应用程序

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

这是为节点/电子写的。 我的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'); }); 

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

如果我做错了什么,请让我知道实施这些方法的正确方法,因为远程和icp结构似乎让我感到困惑。

谢谢。

您需要将另一个parameter passing给index.js文件的侦听进程ipc.on ,使其成为如下所示:

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

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