網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
electron功能實(shí)現(xiàn)---添加全局快捷鍵、開(kāi)機(jī)自啟、選擇安裝路徑
作者:小劉踩坑與知識(shí)點(diǎn)記錄 更新時(shí)間: 2022-08-13 編程語(yǔ)言一、注冊(cè)全局快捷鍵
采用globalShortcut,是主進(jìn)程
ps:快捷鍵方式是全局的‘即使應(yīng)用程序沒(méi)有鍵盤(pán)焦點(diǎn),它也仍然在持續(xù)監(jiān)聽(tīng)鍵盤(pán)事件。在app模塊的ready事件就緒之前,這個(gè)模塊不能使用。
import {
app,
BrowserWindow,
ipcMain,
globalShortcut,
} from "electron";
// 主窗口函數(shù)
createMainWindow() {
globalShortcut.register("ctrl+F5", () => {
app.relaunch();
app.exit();
});
globalShortcut.register("ctrl+F4", () => {
this.mainWindow?.reload();
});
globalShortcut.register("ctrl+F6", () => {
app.exit();
});
globalShortcut.register("F11", () => {
// 是否全屏
if (this.mainWindow?.isFullScreen()) {
// this.mainWindow?.minimize();
this.mainWindow?.setFullScreen(false);
this.mainWindow?.setMenuBarVisibility(true);
} else {
this.mainWindow?.setFullScreen(true);
this.mainWindow?.setMenuBarVisibility(false);
}
});
globalShortcut.register("ctrl+F12", () => {
this.mainWindow?.webContents.openDevTools({ mode: "detach" });
});
}
app.on('will-quit', () => {
// 注銷快捷鍵
globalShortcut.unregister('CommandOrControl+X')
// 注銷所有快捷鍵
globalShortcut.unregisterAll()
})
二、開(kāi)機(jī)自啟
在主進(jìn)程實(shí)現(xiàn)
electron加入開(kāi)機(jī)啟動(dòng)項(xiàng)最核心的代碼是:
app.setLoginItemSettings();
function onAppReady() {
new InitWindow().initWindow();
// 設(shè)置開(kāi)機(jī)自起
const exeName = path.basename(process.execPath);
app.setLoginItemSettings({
// 設(shè)置為true注冊(cè)開(kāi)機(jī)自起
openAtLogin: false, /
openAsHidden: false, //macOs
path: process.execPath,
args: ["--processStart", `"${exeName}"`],
});
}
app.isReady() ? onAppReady() : app.on("ready", onAppReady);
settings是個(gè)object類型,其key有:
- openAtLogin:Boolean(可選)為true時(shí),開(kāi)啟開(kāi)機(jī)自啟動(dòng)功能,默認(rèn)為false。
- openAsHidden:Boolean(可選)。在os系統(tǒng)中因?yàn)闆](méi)有args參數(shù),才使用該屬性實(shí)現(xiàn)。為true時(shí)表示以隱藏的方式啟動(dòng)應(yīng)用。默認(rèn)為false,開(kāi)機(jī)會(huì)啟動(dòng)并彈出該應(yīng)用窗口,并不友好。該屬性在windows系統(tǒng)中的實(shí)現(xiàn)方式如下:
args: ["--openAsHidden"]
設(shè)置開(kāi)機(jī)啟動(dòng)時(shí),在args
中傳入--openAsHidden
,這個(gè)字符串可以隨便更改。獲取開(kāi)機(jī)啟動(dòng)時(shí),也要在args
中傳入同樣的字符串,不然獲取不到正確的值。
然后在顯示主窗口時(shí),先判斷一下
process.argv
中是否包含--openAsHidden
,如果包含,說(shuō)明是開(kāi)機(jī)自動(dòng)啟動(dòng)的,這時(shí)候不顯示窗口;相反 如果不包含--openAsHidden
的話,說(shuō)明是用戶手動(dòng)啟動(dòng)軟件,這時(shí)正常顯示窗口就好了:
win.once("ready-to-show", () => {
if (process.argv.indexOf("--openAsHidden") < 0)
win.show();
});
- path:string(可選),windows在登錄時(shí)啟動(dòng)的可執(zhí)行文件?,默認(rèn)為process.execPath。
- args:strng windows:要傳遞給可執(zhí)行文件的命令行參數(shù),默認(rèn)使用空數(shù)組。注意用引號(hào)將路徑換行。
如果需要在 Windows 上使用Squirrel的?autoUpdater
?,你需要將啟動(dòng)路徑設(shè)置為 Update.exe,并傳遞指定應(yīng)用程序名稱的參數(shù)。 例如:
const appFolder = path.dirname(process.execPath)
const updateExe = path.resolve(appFolder, '..', 'Update.exe')
const exeName = path.basename(process.execPath)
app.setLoginItemSettings({
openAtLogin: true,
path: updateExe,
args: [
'--processStart', `"${exeName}"`,
'--process-start-args', `"--hidden"`
]
})
原文鏈接:https://blog.csdn.net/weixin_46545002/article/details/126305013
相關(guān)推薦
- 2022-05-13 Scrapy-middlewares對(duì)象
- 2022-06-13 matplotlib繪制餅圖的基本配置(萬(wàn)能模板案例)_python
- 2022-05-13 CLion 中文輸出亂碼
- 2024-03-05 git的使用
- 2022-06-07 Python批量解壓&壓縮文件夾的示例代碼_python
- 2023-10-16 清理linux日志
- 2022-10-10 GO必知必會(huì)的常見(jiàn)面試題匯總_Golang
- 2022-04-22 R語(yǔ)言繪制Radar?chart雷達(dá)圖_R語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支