網(wǎng)站首頁 編程語言 正文
一、注冊全局快捷鍵
采用globalShortcut,是主進(jìn)程
ps:快捷鍵方式是全局的‘即使應(yīng)用程序沒有鍵盤焦點(diǎn),它也仍然在持續(xù)監(jiān)聽鍵盤事件。在app模塊的ready事件就緒之前,這個模塊不能使用。
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()
})
二、開機(jī)自啟
在主進(jìn)程實(shí)現(xiàn)
electron加入開機(jī)啟動項(xiàng)最核心的代碼是:
app.setLoginItemSettings();
function onAppReady() {
new InitWindow().initWindow();
// 設(shè)置開機(jī)自起
const exeName = path.basename(process.execPath);
app.setLoginItemSettings({
// 設(shè)置為true注冊開機(jī)自起
openAtLogin: false, /
openAsHidden: false, //macOs
path: process.execPath,
args: ["--processStart", `"${exeName}"`],
});
}
app.isReady() ? onAppReady() : app.on("ready", onAppReady);
settings是個object類型,其key有:
- openAtLogin:Boolean(可選)為true時(shí),開啟開機(jī)自啟動功能,默認(rèn)為false。
- openAsHidden:Boolean(可選)。在os系統(tǒng)中因?yàn)闆]有args參數(shù),才使用該屬性實(shí)現(xiàn)。為true時(shí)表示以隱藏的方式啟動應(yīng)用。默認(rèn)為false,開機(jī)會啟動并彈出該應(yīng)用窗口,并不友好。該屬性在windows系統(tǒng)中的實(shí)現(xiàn)方式如下:
args: ["--openAsHidden"]
設(shè)置開機(jī)啟動時(shí),在args
中傳入--openAsHidden
,這個字符串可以隨便更改。獲取開機(jī)啟動時(shí),也要在args
中傳入同樣的字符串,不然獲取不到正確的值。
然后在顯示主窗口時(shí),先判斷一下
process.argv
中是否包含--openAsHidden
,如果包含,說明是開機(jī)自動啟動的,這時(shí)候不顯示窗口;相反 如果不包含--openAsHidden
的話,說明是用戶手動啟動軟件,這時(shí)正常顯示窗口就好了:
win.once("ready-to-show", () => {
if (process.argv.indexOf("--openAsHidden") < 0)
win.show();
});
- path:string(可選),windows在登錄時(shí)啟動的可執(zhí)行文件?,默認(rèn)為process.execPath。
- args:strng windows:要傳遞給可執(zhí)行文件的命令行參數(shù),默認(rèn)使用空數(shù)組。注意用引號將路徑換行。
如果需要在 Windows 上使用Squirrel的?autoUpdater
?,你需要將啟動路徑設(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-07-08 關(guān)于Python與Golang語言的對比分析_其它綜合
- 2023-05-03 C++類與對象的基礎(chǔ)知識點(diǎn)詳細(xì)分析_C 語言
- 2024-03-13 TypeError: Cannot read property ‘get‘ of undefined
- 2022-08-15 關(guān)于PL/SQL進(jìn)行更新操作時(shí)卡死的解決辦法
- 2021-12-03 C++中signed?main和int?main的區(qū)別_C 語言
- 2022-05-17 基于Python編寫簡易文字語音轉(zhuǎn)換器_python
- 2023-01-01 用幾行C#代碼實(shí)現(xiàn)定時(shí)關(guān)機(jī)/重啟(超詳細(xì)!建議新手練習(xí))_C#教程
- 2022-09-02 useEffect支持async及await使用方式_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- 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)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤: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)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支