Home

Awesome

global_shortcuts

A macOS plugin which can register a callback for a global keyboard shortcut.

As the shortcut is global, the callback will be triggered even when the app does not have focus.

Installation

dependencies:
  global_shortcuts: any

Usage

Register a callback for a given ShortcutKey and zero or more ShortcutModifier

Future<void> register() async {
  try {
    await GlobalShortcuts.register(
      key: ShortcutKey.r,
      modifiers: [ShortcutModifier.shift, ShortcutModifier.command],
      onKeyCombo: _onKeyCombo,
    );
  } on Exception catch (_) {}
}

void _onKeyCombo() => print('Shortcut Pressed at ${DateTime.now()}');

Shortcut can be unregistered using

Future<void> unregister() async {
  try {
    await GlobalShortcuts.unregister();
  } on Exception catch (_) {}
}

Only one shortcut combo can be registered. If, for instance, CTRL+SPACE is registered after SHIFT+CMD+R, then the callback for SHIFT+CMD+R will no longer be invoked.

Notes

Collaboration

Spotted any problems? Please open an issue on GitHub! Would like to work on a windows or linux version? Fork the repo and submit a PR!