Electron的主进程mian.js代码中 添加ipc通道消息:
ipcMain.on('open-osk', async (event) => {
await shell.openPath('osk');
//异步返回消息
event.reply("osk-start",[true])
});
js发消息调用:
function openosk(){
window.electron?.ipcRenderer.send("osk");
}
安装electron-edge-js库:
npm install electron-edge-js
const edge = require('electron-edge-js');
// 自定义C#函数、执行cmd 命令-如打开软键盘
const openWindowsKeyboard = edge.func(`
async (input) => {
// 引入System.Diagnostics命名空间
var process = new System.Diagnostics.Process();
// 设置要执行的命令和参数
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/C osk"; // osk为打开软键盘的命令
// 启动进程
process.Start();
// 等待进程退出并返回结果
process.WaitForExit();
return null;
}
`);
//Electron ipcMain 调用方法:
ipcMain.on('open-osk', async (event) => {
openWindowsKeyboard(null, (error, result) => {
console.log(error,result);
if (error) {
console.error(error);
event.reply("osk-start",[false])
} else {
event.reply("osk-start",[true])
console.log("Windows is open");
}
});
});