簡體   English   中英

無法使用 node/electron javascript 應用程序中的發送功能讓 globalShortcut 將命令注冊到 index.js

[英]Trouble getting globalShortcut to register commands to index.js using send function in node/electron javascript app

出於某種原因,我的代碼編譯沒有錯誤,但是,我的消息“Helloworld”沒有在控制台中正確顯示。 但是,當我按下綁定的組合鍵時,會顯示我的測試消息。 下面是我的一組代碼,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 結構對我來說似乎很困惑。

謝謝你。

您需要將另一個參數傳遞到 index.js 文件的偵聽進程ipc.on中,使其像這樣:

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

有關更多信息,請訪問webContents API 文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM