简体   繁体   English

无法使用 node/electron javascript 应用程序中的发送功能让 globalShortcut 将命令注册到 index.js

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

for some reason my code is compiling with no error, however, my message "Helloworld" is not displaying properly in the console.出于某种原因,我的代码编译没有错误,但是,我的消息“Helloworld”没有在控制台中正确显示。 however my test message is being displayed when i press the bound key combinations.但是,当我按下绑定的组合键时,会显示我的测试消息。 below is my set of code, index.js and main.js下面是我的一组代码,index.js 和 main.js

This is written for node/electron.这是为节点/电子编写的。 My main.js file:我的 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(); });

below is my entire index.js:下面是我的整个 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'); });

the problem i'm having is when i press the keyboard binds, only the console logs from the main.js file are being send to the console.我遇到的问题是当我按下键盘绑定时,只有 main.js 文件中的控制台日志被发送到控制台。 the close command is still working within the rendered window, but any other commands in the index.js window are not being bound to the proper main.js elements. close 命令仍在呈现的窗口中工作,但 index.js 窗口中的任何其他命令都没有绑定到正确的 main.js 元素。

if i am doing something wrong, please let me know the proper way to implement these methodologies, as the remote and icp structures seem to be confusing to me.如果我做错了什么,请告诉我实施这些方法的正确方法,因为远程和 ICP 结构对我来说似乎很困惑。

Thank you.谢谢你。

You need to have another argument passed into the listening process ipc.on of index.js file, make it something like this:您需要将另一个参数传递到 index.js 文件的侦听进程ipc.on中,使其像这样:

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

for more info, visit webContents API docs有关更多信息,请访问webContents API 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Electron:在与 index.js 不同的 javascript 中调用函数 - Electron: Call a function in a different javascript from index.js 电子找不到模块/resources/app/index.js - Electron Cannot find module /resources/app/index.js 如何将数据从 angular 应用程序组件发送到 index.js(节点服务器) - How to send data from angular app components to index.js(node server) 如何将节点获取到 electron 中 index.js 以外的 other.js 文件中 - How to get node into other .js files other than index.js in electron 将数组从 javascript 文件发送到 Express 中的 index.js - Send array from a javascript file to index.js in Express 如何在用 Node.js 编写的 index.js 中调用 function - How to invoke a function in index.js written in Node.js 节点我的应用程序如何找到index.js - node how can My app find index.js 错误 [ERR_REQUIRE_ESM]:不支持来自 /app/commands/Image/meme.js 的 ES 模块 /app/node_modules/got/dist/source/index.js 的 require() - Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/got/dist/source/index.js from /app/commands/Image/meme.js not supported 使用Node打开index.js会引发错误 - Using Node to open index.js throws error 使用node.js / electron / javascript在网页中运行异步命令 - Run asynchronous commands in a webpage with node.js / electron / javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM