Windows support is here! 🔥 Read the full announcement →
⚠️ NativePHP is currently in alpha. We do not recommend distributing production releases of your applications yet.
Global Hotkeys

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;
3use Native\Laravel\Facades\GlobalShortcut;
5class NativeAppServiceProvider
7 public function boot(): void
8 {
9 GlobalShortcut::key('CmdOrCtrl+Shift+A')
10 ->event(\App\Events\MyShortcutEvent::class)
11 ->register();
13 // Additional code, such as registering a menu, opening windows, etc.
14 }

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:

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:

2 ->unregister();

Available modifiers

  • Command or Cmd
  • Control or Ctrl
  • CommandOrControl or CmdOrCtrl
  • Alt
  • Option
  • AltGr
  • Shift
  • Super
  • Meta

Available key codes

  • 0 to 9
  • A to Z
  • F1 to F24
  • Backspace
  • Delete
  • Insert
  • Return or Enter
  • Up, Down, Left and Right
  • Home and End
  • PageUp and PageDown
  • Escape or Esc
  • VolumeUp, VolumeDown and VolumeMute
  • MediaNextTrack, MediaPreviousTrack, MediaStop and MediaPlayPause
  • PrintScreen
  • Numlock
  • Scrolllock
  • Space
  • Plus