Electron 系統快捷鍵globalShortcut的使用


簡介

在應用程序沒有鍵盤焦點時,監聽鍵盤事件。

進程:主進程

globalShortcut 模塊可以在操作系統中注冊/注銷全局快捷鍵, 以便可以為操作定制各種快捷鍵。

注意: 快捷方式是全局的; 即使應用程序沒有鍵盤焦點, 它也仍然在持續監聽鍵盤事件。 This module cannot be used before the ready event of the app module is emitted.

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

app.whenReady().then(() => {
  // Register a 'CommandOrControl+X' shortcut listener.
  const ret = globalShortcut.register('CommandOrControl+X', () => {
    console.log('CommandOrControl+X is pressed')
  })

  if (!ret) {
    console.log('registration failed')
  }

  // 檢查快捷鍵是否注冊成功
  console.log(globalShortcut.isRegistered('CommandOrControl+X'))
})

app.on('will-quit', () => {
  // 注銷快捷鍵
  globalShortcut.unregister('CommandOrControl+X')

  // 注銷所有快捷鍵
  globalShortcut.unregisterAll()
})

方法

globalShortcut 模塊具有以下方法:

EN

globalShortcut.register(accelerator, callback)

返回Boolean - 快捷鍵注冊是否成功

注冊 accelerator 的全局快捷鍵。 當用戶按下注冊快捷鍵時, callbak 會被調用。

如果指定的快捷鍵已經被其他應用程序注冊掉, 調用會默默失敗。 該特性由操作系統定義,因為操作系統不希望多個程序的全局快捷鍵互相沖突。

在 macOS 10.14 Mojave 下面,如果 app 沒有被授權為可信任使用的客戶端,那么下列快捷鍵會注冊失敗:

  • "Media Play/Pause"
  • "Media Next Track"
  • "Media Previous Track"
  • "Media Stop"

EN

globalShortcut.registerAll(accelerators, callback)

  • accelerators String[] - an array of Accelerators.
  • callback Function

Registers a global shortcut of all accelerator items in accelerators. The callback is called when any of the registered shortcuts are pressed by the user.

如果定義的快捷鍵已經被其他應用占有,這個調用會故障沉默。 該特性由操作系統定義,因為操作系統不希望多個程序的全局快捷鍵互相沖突。

在 macOS 10.14 Mojave 下面,如果 app 沒有被授權為可信任使用的客戶端,那么下列快捷鍵會注冊失敗:

  • "Media Play/Pause"
  • "Media Next Track"
  • "Media Previous Track"
  • "Media Stop"

EN

globalShortcut.isRegistered(accelerator)

Returns Boolean - 表示 accelerator 全局快捷鍵是否注冊成功

當快捷鍵已經被其他應用程序注冊時, 此調用將返回 false。 該特性由操作系統定義,因為操作系統不希望多個程序的全局快捷鍵互相沖突。

EN

globalShortcut.unregister(accelerator)

注銷 accelerator 的全局快捷鍵。

EN

globalShortcut.unregisterAll()

注銷所有的全局快捷鍵(清空該應用程序的全局快捷鍵)。

案例

主進程中注冊一個ctrl+i的快捷鍵

主線程

//為了管理應用程序的生命周期事件以及創建和控制瀏覽器窗口,您從 electron 包導入了 app 和 BrowserWindow 模塊 。
const { app, BrowserWindow,globalShortcut } = require('electron')

//在此之后,你定義了一個創建 新的瀏覽窗口的函數並將 nodeIntegration 設置為 true,將 index.html 文件加載到窗口中(第 12 行,稍后我們將討論該文件)
function createWindow () {
    const win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            //是否注入nodeapi
            nodeIntegration: true,
            //渲染進程是否啟用remote模塊
            enableRemoteModule: true
        }
    })

    win.loadFile('index.html')
}

//你通過調用 createWindow方法,在 electron app 第一次被初始化時創建了一個新的窗口。
app.whenReady().then(createWindow)

//當 Electron 完成初始化時,發出一次
app.on('ready', ()=>{
    //CommandOrControl 表示系統兼容 win是Control mac是Command
    const ret = globalShortcut.register('CommandOrControl+i', () => {
        console.log('you pressed ctrl+i')
    })

    if (!ret) {
        console.log('registration failed')
    }

    // 檢查快捷鍵是否注冊成功
    console.log('Whether the registration is successful:'+globalShortcut.isRegistered('CommandOrControl+i'))
})

//您添加了一個新的偵聽器,當應用程序不再有任何打開窗口時試圖退出。 由於操作系統的 窗口管理行為 ,此監聽器在 macOS 上是禁止操作的
app.on('window-all-closed', () => {

    // 注銷所有快捷鍵
    globalShortcut.unregisterAll()


    if (process.platform !== 'darwin') {
        app.quit()
    }
})

//您添加一個新的偵聽器,只有當應用程序激活后沒有可見窗口時,才能創建新的瀏覽器窗口。 例如,在首次啟動應用程序后或重啟運行中的應用程序
app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
        createWindow()
    }
})

image-20210204143530888

渲染進程中注冊一個ctrl+g快捷鍵

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>dialog 對話框</title>
</head>
<body>
<h1>系統快捷鍵</h1>
<button onclick="register()">點擊注冊ctrl+g快捷鍵</button>
</body>
<script>
    const {globalShortcut} = require('electron').remote;
    function register(){
        const ret = globalShortcut.register('CommandOrControl+g', () => {
            console.log('你按下了 ctrl+g')
        })

        if (!ret) {
            console.log('registration failed')
        }

        // 檢查快捷鍵是否注冊成功
        console.log('是否注冊成功:'+globalShortcut.isRegistered('CommandOrControl+g'))
    }
</script>
</html>

image-20210204144244226


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM