Electron 上一窗口焦点/电子
目前我正为一个棘手的问题挠头。我只是从电子开始,到目前为止还不错。但是,当窗口被隐藏时(它是一个弹出窗口,显示快捷方式,当你按enter键时,快捷方式消失),我希望将焦点返回到上一个窗口 我使用的是Mac,菜单栏上显示了我以前的应用程序的名称,所以看起来焦点回到了应用程序,但并不完全是因为窗口没有被选中 知道怎么解决吗Electron 上一窗口焦点/电子,electron,Electron,目前我正为一个棘手的问题挠头。我只是从电子开始,到目前为止还不错。但是,当窗口被隐藏时(它是一个弹出窗口,显示快捷方式,当你按enter键时,快捷方式消失),我希望将焦点返回到上一个窗口 我使用的是Mac,菜单栏上显示了我以前的应用程序的名称,所以看起来焦点回到了应用程序,但并不完全是因为窗口没有被选中 知道怎么解决吗 谢谢 我刚刚在Github上发布了一个名为的电子测试应用程序,它显示了如何正确地将焦点返回到上一个窗口。它是我以前的一个项目的简化版本,仅限于macOS。我遇到了与您完全相同的问
谢谢 我刚刚在Github上发布了一个名为的电子测试应用程序,它显示了如何正确地将焦点返回到上一个窗口。它是我以前的一个项目的简化版本,仅限于macOS。我遇到了与您完全相同的问题,我记得我通过隐藏应用程序而不是窗口,并处理窗口模糊事件来实际隐藏它来解决它 嗯 main.js:
const { app, BrowserWindow, globalShortcut, ipcMain } = require ('electron');
let mainWindow = null;
function onAppReady ()
{
mainWindow = new BrowserWindow
(
{
width: 600,
height: 600,
show: false,
frame: false
}
);
mainWindow.loadURL (`file://${__dirname}/index.html`);
mainWindow.once ('closed', () => { mainWindow = null; });
mainWindow.on ('blur', () => { mainWindow.hide (); });
globalShortcut.register ("CommandOrControl+Alt+P", () => { mainWindow.show (); });
ipcMain.on ('dismiss', () => { app.hide (); });
}
app.once ('ready', onAppReady);
app.once ('window-all-closed', () => { app.quit (); });
app.dock.hide ();
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<!-- All of the Node.js APIs are available in this renderer process. -->
We are using Node.js <script>document.write(process.versions.node)</script>,
Chromium <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
<script>
// You can also require other files to run in this process
require('./renderer.js')
</script>
</body>
</html>
const { ipcRenderer } = require ('electron');
document.addEventListener
(
'keydown',
(event) =>
{
if (event.key === 'Enter')
{
event.preventDefault ();
ipcRenderer.send ('dismiss');
}
}
);
index.html:
const { app, BrowserWindow, globalShortcut, ipcMain } = require ('electron');
let mainWindow = null;
function onAppReady ()
{
mainWindow = new BrowserWindow
(
{
width: 600,
height: 600,
show: false,
frame: false
}
);
mainWindow.loadURL (`file://${__dirname}/index.html`);
mainWindow.once ('closed', () => { mainWindow = null; });
mainWindow.on ('blur', () => { mainWindow.hide (); });
globalShortcut.register ("CommandOrControl+Alt+P", () => { mainWindow.show (); });
ipcMain.on ('dismiss', () => { app.hide (); });
}
app.once ('ready', onAppReady);
app.once ('window-all-closed', () => { app.quit (); });
app.dock.hide ();
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<!-- All of the Node.js APIs are available in this renderer process. -->
We are using Node.js <script>document.write(process.versions.node)</script>,
Chromium <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
<script>
// You can also require other files to run in this process
require('./renderer.js')
</script>
</body>
</html>
const { ipcRenderer } = require ('electron');
document.addEventListener
(
'keydown',
(event) =>
{
if (event.key === 'Enter')
{
event.preventDefault ();
ipcRenderer.send ('dismiss');
}
}
);
对于Linux:
我发现browserWindow.hide()
正确地恢复了焦点
对于Windows:
browserWindow.minimize()
正确恢复焦点
对于Mac:
app.hide()
正确恢复焦点。注意:很遗憾,调用app.hide()
会隐藏所有窗口。使用app.hide()
这在Mac、Linux和Windows上都适用:
hide() {
this.window.minimize();
this.window.hide();
if (process.platform == "darwin") this.app.hide()
}
show() {
this.window.show();
this.window.restore();
}
添加这个对我来说很有用:
mainWindow.on('blur', () => app.hide())
我使用main window.hide()
隐藏应用程序,在模糊侦听器上添加app.hide()
,将焦点返回到上次使用的应用程序。您的代码是什么样子的?您正在呼叫win.show()
?这正是我要找的!隐藏应用程序本身是一条出路。太棒了,你让我开心了:)