galactic_hotkeys 1.1.1 copy "galactic_hotkeys: ^1.1.1" to clipboard
galactic_hotkeys: ^1.1.1 copied to clipboard

Galactic Hotkeys is a robust Flutter package for streamlined keyboard shortcuts in your apps. Inspired by the cosmos.

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:galactic_hotkeys/galactic_hotkeys_widget.dart';

void main() {
  runApp(const ExampleGalacticHotkeys());
}

class ExampleGalacticHotkeys extends StatelessWidget {
  const ExampleGalacticHotkeys({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Galactic Hotkeys Example',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const _HomePage(),
    );
  }
}

enum Shortcut {
  undo,
  redo,
  cut,
  copy,
  paste,
}

class _HomePage extends StatefulWidget {
  const _HomePage({super.key});

  @override
  State<_HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<_HomePage> {
  Shortcut? _latestPressedShortcut;
  Timer? _delayToClearShortcutPressed;

  @override
  void dispose() {
    _delayToClearShortcutPressed?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Galactic Hotkeys'),
      ),
      body: GalacticHotkeys<Shortcut>(
        shortcuts: const {
          Shortcut.undo: [
            [LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.keyZ],
            [LogicalKeyboardKey.metaLeft, LogicalKeyboardKey.keyZ],
          ],
          Shortcut.redo: [
            [LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.keyY],
            [LogicalKeyboardKey.metaLeft, LogicalKeyboardKey.keyY],
            [
              LogicalKeyboardKey.controlLeft,
              LogicalKeyboardKey.shiftLeft,
              LogicalKeyboardKey.keyZ
            ],
            [
              LogicalKeyboardKey.metaLeft,
              LogicalKeyboardKey.shiftLeft,
              LogicalKeyboardKey.keyZ
            ],
          ],
          Shortcut.cut: [
            [LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.keyX],
            [LogicalKeyboardKey.metaLeft, LogicalKeyboardKey.keyX],
          ],
          Shortcut.copy: [
            [LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.keyC],
            [LogicalKeyboardKey.metaLeft, LogicalKeyboardKey.keyC],
          ],
          Shortcut.paste: [
            [LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.keyV],
            [LogicalKeyboardKey.metaLeft, LogicalKeyboardKey.keyV],
          ],
        },
        onShortcutPressed: (Shortcut shortcut, pressedKeys) {
          setState(() => _latestPressedShortcut = shortcut);

          _delayToClearShortcutPressed?.cancel();
          _delayToClearShortcutPressed = Timer(const Duration(seconds: 2), () {
            if (mounted) {
              setState(() => _latestPressedShortcut = null);
            }
          });
          // switch (shortcut) {
          //   case Shortcut.undo:
          //     break;
          //   case Shortcut.redo:
          //     break;
          //   case Shortcut.cut:
          //     break;
          //   case Shortcut.copy:
          //     break;
          //   case Shortcut.paste:
          //     break;
          // }
        },
        child: ListView(
          children: [
            for (final shortcut in Shortcut.values)
              ListTile(
                title: Text(
                  shortcut.name,
                  style: TextStyle(
                    color:
                        shortcut == _latestPressedShortcut ? Colors.red : null,
                  ),
                ),
              ),
          ],
        ),
      ),
    );
  }
}
1
likes
160
points
38
downloads

Publisher

verified publishercosmos-in-code.com

Weekly Downloads

Galactic Hotkeys is a robust Flutter package for streamlined keyboard shortcuts in your apps. Inspired by the cosmos.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter, provider

More

Packages that depend on galactic_hotkeys