久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用全局快捷方式(如 Spotlight/Launchy)將電子應用程

Bringing an electron app to foreground with a global shortcut (like Spotlight/Launchy)(使用全局快捷方式(如 Spotlight/Launchy)將電子應用程序帶到前臺)
本文介紹了使用全局快捷方式(如 Spotlight/Launchy)將電子應用程序帶到前臺的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我希望復制類似于 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進程中創建子窗口時如何修復 BrowserWindow 不是構造函數錯誤) - IT屋-程序員軟件開發技術
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個 loadURL 上執行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲?)
How to access camera/webcamera inside electron app?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 色综合天天综合网国产成人网 | 亚洲一区在线视频 | 免费av不卡| 日韩色黄大片 | 亚洲二区在线观看 | 免费的一级片 | 国产a区 | 日韩精品一区二区在线 | 成人女同在线观看 | 国产成人免费在线观看 | 天天色天天干天天 | 日韩在线中文 | 亚洲综合视频在线观看 | 性爱免费视频 | 婷婷综合五月天 | 999成人网| 亚洲一区二区在线 | 一区二区三区四区在线播放 | 久久久在线视频 | av网站免费在线观看 | 国产一区二区自拍 | 夜夜精品视频 | 日韩av在线网址 | 欧美日韩国产二区 | 久久99精品久久久久久琪琪 | 中国免费av | 51调教丨国产调教视频 | 激情小说在线视频 | 国产操操操 | 亚洲精品a | 91久久国产综合久久91精品网站 | 美女久久久久 | 天天躁日日躁狠狠躁 | 青青草免费在线 | av三级在线观看 | 久久久久久久九九九九 | 黄色免费一级片 | 四虎影院在线播放 | 玖草在线 | 99精品热| 午夜国产一区 |