global_shortcuts 0.0.1+1 copy "global_shortcuts: ^0.0.1+1" to clipboard
global_shortcuts: ^0.0.1+1 copied to clipboard

PlatformmacOS

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

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:global_shortcuts/global_shortcuts.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var _key = ShortcutKey.space;
  var _modifiers = [ShortcutModifier.control];
  var _isRegistered = false;
  var _outputString = '';

  Future<void> _register() async {
    late bool success;

    try {
      success = await GlobalShortcuts.register(
            key: _key,
            modifiers: _modifiers,
            onKeyCombo: _onKeyCombo,
          ) ??
          false;
    } on Exception catch (e) {
      print('An exception occurred while trying to register');
      print(e);
      success = false;
    }

    if (success != _isRegistered) {
      if (mounted) {
        setState(() => _isRegistered = success);
      }
    }
  }

  Future<void> _unregister() async {
    try {
      await GlobalShortcuts.unregister();
    } on Exception catch (e) {
      print('An exception occurred while trying to unregister');
      print(e);
    }

    if (mounted) {
      setState(() {
        _isRegistered = false;
        _outputString = '';
      });
    }
  }

  void _onKeyCombo() {
    if (mounted) {
      setState(() => _outputString = 'Shortcut Pressed at ${DateTime.now()}');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('global_shortcuts'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  DropdownButton<ShortcutModifier>(
                    items: [
                      for (final modifier in ShortcutModifier.values)
                        DropdownMenuItem(
                          value: modifier,
                          child: Text(modifier.asString),
                        ),
                    ],
                    value: _modifiers.first,
                    onChanged: (newValue) {
                      if (newValue != null) {
                        if (_isRegistered) {
                          _unregister();
                        }
                        setState(() => _modifiers = [newValue]);
                      }
                    },
                  ),
                  DropdownButton<ShortcutKey>(
                    items: [
                      for (final key in ShortcutKey.values)
                        DropdownMenuItem(
                          value: key,
                          child: Text(key.asString),
                        ),
                    ],
                    value: _key,
                    onChanged: (newValue) {
                      if (newValue != null) {
                        if (_isRegistered) {
                          _unregister();
                        }
                        setState(() => _key = newValue);
                      }
                    },
                  ),
                ],
              ),
              if (!_isRegistered)
                ElevatedButton(
                  onPressed: () async => _register(),
                  child: Text('Register'),
                ),
              if (_isRegistered)
                ElevatedButton(
                  onPressed: () async => _unregister(),
                  child: Text('Unregister'),
                ),
              Text(_outputString),
            ],
          ),
        ),
      ),
    );
  }
}
8
likes
130
pub points
57%
popularity

Publisher

verified publisherdefuncart.com

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

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on global_shortcuts