使用全球快捷方式将电子应用程序引导至前景(如Spotlight / Launchy)(Bringing an electron app to foreground with a global shor

编程入门 行业动态 更新时间:2024-10-25 04:16:45
使用全球快捷方式将电子应用程序引导至前景(如Spotlight / Launchy)(Bringing an electron app to foreground with a global shortcut (like Spotlight/Launchy))

我正在寻找复制类似于Launchy / Quicksilver / Spotlight的行为。

我想要一个始终运行的电子应用程序。 当我点击一个快捷键时,电子应用程序被带到前台并聚焦。

我明白globalShortcut模块可以用来绑定一个快捷方式,但是我不知道如何使这个快捷方式触发应用程序到前台。

任何帮助将非常感激...

I'm looking to replicate behavior similar to that of Launchy/Quicksilver/Spotlight.

I want to have an electron app that's always running. When I hit a shortcut key, the electron app is brought to the foreground and to focus.

I understand that the globalShortcut module can be used to bind a shortcut, however I can't figure out how to make that shortcut trigger bringing the app to the foreground.

Any help would be much appreciated...

最满意答案

让我们从最简单的案例开始,然后构建我们的解决方案,以更好地处理一些边缘案例。

最简单的情况是显示一个当我们注册的全局快捷键被按下时已经打开的窗口。

const path = require('path'); const { app, BrowserWindow, globalShortcut } = require('electron'); let mainWindow = null; app.on('ready', () => { mainWindow = new BrowserWindow(); mainWindow.loadURL(path.join(__dirname, 'index.html')); const shortcut = globalShortcut.register('Control+Space', () => { mainWindow.show(); }); if (!shortcut) { console.log('Registration failed.'); } });

但是这段代码有一些问题。 好消息是,如果窗口已经最小化,它仍然有效。 坏消息是,如果窗口关闭,它将不起作用。 这是因为关闭最后一个窗口会退出应用程序。 游民。 (坦率地说,我对此有点惊讶 - 但是那就是发生了什么,所以,让我们一起去吧。)

让我们停止发生。

app.on('window-all-closed', (event) => { event.preventDefault(); });

好的,我们的应用不会退出,但它会崩溃。

Uncaught Exception: Error: Object has been destroyed

好,很好。 这是因为窗户靠近时会被破坏。 所以,我们不要关闭它。 让我们把它藏起来吧? 在app.on('ready', () => {…}) ,添加以下内容:

mainWindow.on('close', (event) => { event.preventDefault(); mainWindow.hide(); });

最终结果如下所示:

const path = require('path'); const { app, BrowserWindow, globalShortcut } = require('electron'); let mainWindow = null; app.on('ready', () => { mainWindow = new BrowserWindow(); mainWindow.loadURL(path.join(__dirname, 'index.html')); const shortcut = globalShortcut.register('Control+Space', () => { mainWindow.show(); }); if (!shortcut) { console.log('Registration failed.'); } mainWindow.on('close', (event) => { event.preventDefault(); mainWindow.hide(); }); }); app.on('window-all-closed', (event) => { event.preventDefault(); });

因此,您应该具备基本的功能。 你按下你的全局快捷键,出现窗口。 关闭它,然后按下键并观察它重新出现。

Let's start with the simplest case and then build our solution to better handle some edge cases.

The simplest possible case is to show a window that is already open whenever the global shortcut we registered is pressed.

const path = require('path'); const { app, BrowserWindow, globalShortcut } = require('electron'); let mainWindow = null; app.on('ready', () => { mainWindow = new BrowserWindow(); mainWindow.loadURL(path.join(__dirname, 'index.html')); const shortcut = globalShortcut.register('Control+Space', () => { mainWindow.show(); }); if (!shortcut) { console.log('Registration failed.'); } });

This code has some problems though. The good news is that it still works if the window has been minimized. The bad news is that it will not work if the window has been closed. This is because closing the last window quits the application. Bummer. (Frankly, I was a little surprised by this—but that's what happens. So, let's go with it.)

Let's stop that from happening.

app.on('window-all-closed', (event) => { event.preventDefault(); });

Okay, our app doesn't quit, it but it crashes.

Uncaught Exception: Error: Object has been destroyed

Alright, fine. This is because the window is destroyed when it's close. So, let's not close it. Let's hide it, shall we? Within app.on('ready', () => {…}), add the following:

mainWindow.on('close', (event) => { event.preventDefault(); mainWindow.hide(); });

The end result looks like this:

const path = require('path'); const { app, BrowserWindow, globalShortcut } = require('electron'); let mainWindow = null; app.on('ready', () => { mainWindow = new BrowserWindow(); mainWindow.loadURL(path.join(__dirname, 'index.html')); const shortcut = globalShortcut.register('Control+Space', () => { mainWindow.show(); }); if (!shortcut) { console.log('Registration failed.'); } mainWindow.on('close', (event) => { event.preventDefault(); mainWindow.hide(); }); }); app.on('window-all-closed', (event) => { event.preventDefault(); });

And with that you should have the basic functionality in place. You press your global shortcut and the window appears. Dismiss it and press the keys and watch it reappear.

更多推荐

本文发布于:2023-07-25 19:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1265170.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:快捷方式   应用程序   前景   全球   电子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!