NativePHP is currently in alpha development
Let's get to beta!- Global Hotkeys
- Registering Hotkeys
- Removing registered hotkeys
- Available modifiers
- Available key codes
#Global Hotkeys
In your NativePHP application, you may define multiple global hotkeys. Unlike hotkeys that you may define in your application via JavaScript, these hotkeys are globally registered. This means that your application will even receive these hotkeys, when it is running in the background and not focused.
As these global hotkeys are usually used in your entire application, a common approach to registering them is inside the NativeAppServiceProvider
class.
1namespace App\Providers; 2 3use Native\Laravel\Facades\GlobalShortcut; 4 5class NativeAppServiceProvider 6{ 7 public function boot(): void 8 { 9 GlobalShortcut::key('CmdOrCtrl+Shift+A')10 ->event(\App\Events\MyShortcutEvent::class)11 ->register();12 13 // Additional code, such as registering a menu, opening windows, etc.14 }15}
#Registering Hotkeys
You may register a global shortcut using the GlobalShortcut
facade.
Using the key
method, you may specify the hotkey to listen for. The hotkey must be a string that contains the modifiers and the key separated by a +
sign.
For example, if you want to register a hotkey that triggers the MyEvent
event when the user presses Cmd+Shift+D
, you may do the following:
1GlobalShortcut::key('Cmd+Shift+D')2 ->event(\App\Events\MyEvent::class)3 ->register();
You can find a list of all available modifiers here.
#Removing registered hotkeys
Sometimes you may want to remove an already registered global hotkey.
To do this, specify the hotkey that you used to register and call the unregister
method on the GlobalShortcut
facade.
You do not need to provide an event class in this case, as every hotkey can only be registered once.
For example, in order to remove the Cmd+Shift+D
global hotkey, you may do the following:
1GlobalShortcut::key('Cmd+Shift+D')2 ->unregister();
#Available modifiers
-
Command
orCmd
-
Control
orCtrl
-
CommandOrControl
orCmdOrCtrl
-
Alt
-
Option
-
AltGr
-
Shift
-
Super
-
Meta
#Available key codes
-
0
to9
-
A
toZ
-
F1
toF24
-
Backspace
-
Delete
-
Insert
-
Return
orEnter
-
Up
,Down
,Left
andRight
-
Home
andEnd
-
PageUp
andPageDown
-
Escape
orEsc
-
VolumeUp
,VolumeDown
andVolumeMute
-
MediaNextTrack
,MediaPreviousTrack
,MediaStop
andMediaPlayPause
-
PrintScreen
-
Numlock
-
Scrolllock
-
Space
-
Plus
- Global Hotkeys
- Registering Hotkeys
- Removing registered hotkeys
- Available modifiers
- Available key codes