如何使用快捷键打开电子应用程序? [英] How to open electron app using shortcut key?

查看:34
本文介绍了如何使用快捷键打开电子应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Mac OS上工作的电子应用程序,但一旦应用程序被移动到应用程序,那么我想有快捷键直接启动,而不是每次都双击。

Like Command+Key[任意组合键]
即使这样也应该适用于Windows

推荐答案

即使应用程序没有键盘焦点,您也可以使用电子邮件globalShortcut module检测键盘事件。

在此示例中,当按下Control+XCommand+X时,应用程序会打开一个浏览器窗口,并防止在窗口关闭以继续在后台侦听快捷方式时退出。

// require Electron
const { app, BrowserWindow, globalShortcut } = require('electron')

function createWindow () {
  // Create the browser window.
  let win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

  // and load the index.html of the app.
  win.loadFile('index.html')
}

// when app is ready register a global shortcut
// that calls createWindow function
app.on('ready', () => {
    globalShortcut.register('CommandOrControl+X', createWindow)
})

// do not quit when all windows are closed
// and continue running on background to listen
// for shortcuts
app.on('window-all-closed', (e) => {
  e.preventDefault()
  e.returnValue = false
})

如果要在启动时启动应用程序,可以使用this package

这篇关于如何使用快捷键打开电子应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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