通过全局快捷键(例如Spotlight / Launchy)将电子应用程序置于前台 [英] Bringing an electron app to foreground with a global shortcut (like Spotlight/Launchy)

查看:98
本文介绍了通过全局快捷键(例如Spotlight / Launchy)将电子应用程序置于前台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望复制类似于Launchy / Quicksilver / Spotlight的行为。



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



我了解可以使用globalShortcut模块绑定快捷方式,但是我不知道如何使该快捷方式触发将应用程序置于前台。 / p>

任何帮助将不胜感激...

解决方案

让我们开始吧



最简单的情况是,只要我们注册了全局快捷方式,就显示一个已经打开的窗口

  const path = require('path'); 
const {app,BrowserWindow,globalShortcut} = require(’electron’);

让mainWindow = null;

app.on('ready',()=> {

mainWindow = new BrowserWindow();
mainWindow.loadURL(path.join(__ dirname ,'index.html'));

const快捷方式= globalShortcut.register('Control + Space',()=> {
mainWindow.show();
});

if(!shortcut){console.log('注册失败。');}

});

此代码虽然存在一些问题。好消息是,如果窗口已最小化,它仍然可以工作。坏消息是,如果窗口已关闭,它将不起作用。这是因为关闭最后一个窗口会退出应用程序。笨蛋(坦率地说,我对此感到有些惊讶-但这就是发生的事情。所以,让我们继续吧。)



让我们阻止这种情况的发生。

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

好吧,我们的应用程序没有退出,但是崩溃了。

 未捕获的异常:
错误:对象已被破坏

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

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

最终结果如下:

  const path = require('path'); 
const {app,BrowserWindow,globalShortcut} = require(’electron’);

让mainWindow = null;

app.on('ready',()=> {

mainWindow = new BrowserWindow();
mainWindow.loadURL(path.join(__ dirname ,'index.html'));

const快捷方式= globalShortcut.register('Control + Space',()=> {
mainWindow.show();
});

if(!shortcut){console.log('注册失败。');}

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

});


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

因此,您应该具备基本功能。您按全局快捷方式,将出现窗口。将其关闭,然后按键,然后重新出现。


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...

解决方案

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.

这篇关于通过全局快捷键(例如Spotlight / Launchy)将电子应用程序置于前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆