Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 746
GlobalShortcut
Register global keyboard shortcuts that work even when the application is not focused.
The Electron.GlobalShortcut API provides the ability to register global keyboard shortcuts that can be triggered even when the application does not have keyboard focus. This is useful for creating system-wide hotkeys and shortcuts.
Check if the accelerator is registered.
Parameters:
accelerator- Keyboard shortcut to check
Returns:
Whether this application has registered the accelerator.
Registers a global shortcut of accelerator. The callback is called when the registered shortcut is pressed by the user.
Parameters:
accelerator- Keyboard shortcut combinationfunction- Callback function to execute when shortcut is pressed
Unregisters the global shortcut of accelerator.
Parameters:
accelerator- Keyboard shortcut to unregister
Unregisters all of the global shortcuts.
// Register global shortcutsElectron.GlobalShortcut.Register("CommandOrControl+N",()=>{CreateNewDocument();});Electron.GlobalShortcut.Register("CommandOrControl+O",()=>{OpenDocument();});Electron.GlobalShortcut.Register("CommandOrControl+S",()=>{SaveDocument();});// Media playback shortcutsElectron.GlobalShortcut.Register("MediaPlayPause",()=>{TogglePlayback();});Electron.GlobalShortcut.Register("MediaNextTrack",()=>{NextTrack();});Electron.GlobalShortcut.Register("MediaPreviousTrack",()=>{PreviousTrack();});// Application control shortcutsElectron.GlobalShortcut.Register("CommandOrControl+Shift+Q",async()=>{varresult=awaitElectron.Dialog.ShowMessageBoxAsync("Quit Application?","Are you sure you want to quit?");if(result.Response==1)// Yes{Electron.App.Quit();}});Electron.GlobalShortcut.Register("CommandOrControl+Shift+H",()=>{ToggleMainWindow();});// Register shortcuts based on user preferencespublicvoidRegisterUserShortcuts(Dictionary<string,Action>shortcuts){foreach(varshortcutinshortcuts){Electron.GlobalShortcut.Register(shortcut.Key,shortcut.Value);}}// Check if shortcut is availablepublicasyncTask<bool>IsShortcutAvailable(stringaccelerator){returnawaitElectron.GlobalShortcut.IsRegisteredAsync(accelerator);}// Unregister specific shortcutpublicvoidUnregisterShortcut(stringaccelerator){Electron.GlobalShortcut.Unregister(accelerator);}// macOS specific shortcutsif(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)){Electron.GlobalShortcut.Register("Command+Comma",()=>{OpenPreferences();});Electron.GlobalShortcut.Register("Command+H",()=>{Electron.App.Hide();});}// Windows/Linux shortcutselse{Electron.GlobalShortcut.Register("Ctrl+Shift+P",()=>{OpenPreferences();});Electron.GlobalShortcut.Register("Alt+F4",()=>{Electron.App.Quit();});}// Validate shortcuts before registrationpublicasyncTask<bool>TryRegisterShortcut(stringaccelerator,Actioncallback){if(awaitElectron.GlobalShortcut.IsRegisteredAsync(accelerator)){Console.WriteLine($"Shortcut {accelerator} is already registered");returnfalse;}try{Electron.GlobalShortcut.Register(accelerator,callback);Console.WriteLine($"Successfully registered shortcut: {accelerator}");returntrue;}catch(Exceptionex){Console.WriteLine($"Failed to register shortcut {accelerator}: {ex.Message}");returnfalse;}}- Electron.App - Application lifecycle events
- Electron.Menu - Menu-based shortcuts
- Electron.WindowManager - Window focus management
- Electron GlobalShortcut Documentation - Official Electron global shortcut API
Want to contribute to this documentation? Please fork and create a PR! The Wiki is autogenerated from the /docs content in the repository.