久久久久久久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?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 亚洲一区二区黄 | 四虎伊人 | 日韩精品在线免费观看视频 | 91福利网 | 中国一级特黄真人毛片免费观看 | 日韩中文字幕av | 99在线免费观看视频 | 久草热线 | 在线视频一区二区 | 欧美精品一区在线 | 成人一区二区电影 | 91精品久久久久久久久 | 91视频88av| 欧美中文字幕一区二区三区亚洲 | 蜜桃毛片 | 国产精品伦一区二区三级视频 | 日韩在线中文字幕 | 日本免费视频在线观看 | 国产精品爱久久久久久久 | 日本在线免费看最新的电影 | 国产精品一区二区视频 | 韩日一区二区 | 成人精品网| 成人一区在线观看 | 国产日韩欧美中文 | 91视视频在线观看入口直接观看 | 色吊丝2288sds中文字幕 | 久久日本| 成年人的视频免费观看 | 亚洲影音先锋 | 欧美亚洲视频在线观看 | 一级毛片视频在线观看 | 亚洲国产精品久久久 | 伊人无码高清 | 中文字幕第一页在线 | 2019天天干夜夜操 | 狠狠婷婷综合久久久久久妖精 | 精品九九九 | www视频在线观看 | 精品福利一区二区三区 | 国产精品久久久久久久久久久久久 |