globalShortcut
アプリケーションにキーボードフォーカスがないときにキーボードイベントを検出します。
Process: Main
globalShortcut
モジュールは、オペレーティングシステムに対してグローバルショートカットを登録/登録解除することができます。そのため、様々なショートカットに対する操作をカスタマイズすることができます。
[!NOTE] The shortcut is global; it will work even if the app does not have the keyboard focus. This module cannot be used before the
ready
event of the app module is emitted. Please also note that it is also possible to use Chromium'sGlobalShortcutsPortal
implementation, which allows apps to bind global shortcuts when running within a Wayland session.
const { app, globalShortcut } = require('electron')
// Enable usage of Portal's globalShortcuts. This is essential for cases when
// the app runs in a Wayland session.
app.commandLine.appendSwitch('enable-features', 'GlobalShortcutsPortal')
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
モジュールには以下のメソッドがあります。
globalShortcut.register(accelerator, callback)
accelerator
Acceleratorcallback
Function
戻り値 boolean
- ショートカットが正常に登録されたかどうか.
accelerator
のグローバルショートカットを登録します。 callback
は登録したショートカットがユーザに押下されたときに呼び出されます。
アクセラレータが他のアプリケーションによってすでに使用されている場合、この呼び出しは何も通知することなく失敗します。 この動作は、アプリケーションにグローバルショートカットの取り合いをさせたくないため、オペレーティングシステムによって意図されたものです。
The following accelerators will not be registered successfully on macOS 10.14 Mojave unless the app has been authorized as a trusted accessibility client:
- "Media Play/Pause"
- "Media Next Track"
- "Media Previous Track"
- "Media Stop"
globalShortcut.registerAll(accelerators, callback)
accelerators
Accelerator[] - an array of Accelerators.callback
Function
accelerators
内の全ての accelerator
のグローバルショートカットを登録します。 callback
は登録したいずれかのショートカットがユーザに押下されたときに呼び出されます。
与えられたアクセラレータが他のアプリケーションによってすでに使用されている場合、この呼び出しは何も通知することなく失敗します。 この動作は、アプリケーションにグローバルショートカットの取り合いをさせたくないため、オペレーティングシステムによって意図されたものです。
The following accelerators will not be registered successfully on macOS 10.14 Mojave unless the app has been authorized as a trusted accessibility client:
- "Media Play/Pause"
- "Media Next Track"
- "Media Previous Track"
- "Media Stop"
globalShortcut.isRegistered(accelerator)
accelerator
Accelerator
戻り値 boolean
- このアプリケーションが accelerator
を登録したかどうか。
アクセラレータが他のアプリケーションによってすでに使用されている場合、この呼び出しは false
を返すはずです。 この動作は、アプリケーションにグローバルショートカットの取り合いをさせたくないため、オペレーティングシステムによって意図されたものです。
globalShortcut.unregister(accelerator)
accelerator
Accelerator
accelerator
のグローバルショートカットを登録解除します。
globalShortcut.unregisterAll()
すべてのグローバルショートカットを登録解除します。