問題描述
我希望復制類似于 Launchy/Quicksilver/Spotlight 的行為.
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.
我知道 globalShortcut 模塊可用于綁定快捷方式,但我不知道如何使該快捷方式觸發將應用程序帶到前臺.
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...
推薦答案
讓我們從最簡單的情況開始,然后構建我們的解決方案以更好地處理一些邊緣情況.
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
好的,好的.這是因為窗口在關閉時會被破壞.所以,我們不要關閉它.讓我們隱藏它,好嗎?在 app.on('ready', () => {…})
中,添加以下內容:
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();
});
最終結果如下所示:
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.
這篇關于使用全局快捷方式(如 Spotlight/Launchy)將電子應用程序帶到前臺的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!