leanflutter/hotkey_manager

Can not response "Ctrl + Shift + Z" if "Ctrl + Z" has been registered first

tgalpha opened this issue · 1 comments

Please try the code sample appended.
If "Ctrl + Shift + Z" is registerd after "Ctrl + Z", it it does not work

I think this is caused by the HotKeyManager._handleRawKeyEvent method using _hotKeyList.firstWhere to match the hotkey.
Maybe you need sort _hotKeyList after register a new hotkey
or match modifierKey strictly in HotKeyManager._handleRawKeyEvent

Code sample
import 'package:flutter/material.dart';
import 'package:hotkey_manager/hotkey_manager.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  hotKeyManager.unregisterAll();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String lastAcceptedHotkey = '';

  HotKey ctrlZ = HotKey(
    KeyCode.keyZ,
    modifiers: [KeyModifier.control],
    scope: HotKeyScope.inapp,
  );

  HotKey ctrlShiftZ = HotKey(
    KeyCode.keyZ,
    modifiers: [KeyModifier.control, KeyModifier.shift],
    scope: HotKeyScope.inapp,
  );

  void _keyUpHandler(HotKey hotkey) {
    lastAcceptedHotkey = hotkey.toString();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          ElevatedButton(
            onPressed: () async {
              await hotKeyManager.unregisterAll();
              hotKeyManager.register(ctrlZ, keyUpHandler: _keyUpHandler);
              hotKeyManager.register(ctrlShiftZ, keyUpHandler: _keyUpHandler);
            },
            child: const Text('Register order: CtrlZ, CtrlShiftZ'),
          ),
          ElevatedButton(
            onPressed: () async {
              await hotKeyManager.unregisterAll();
              hotKeyManager.register(ctrlShiftZ, keyUpHandler: _keyUpHandler);
              hotKeyManager.register(ctrlZ, keyUpHandler: _keyUpHandler);
            },
            child: const Text('Register order: CtrlShiftZ, CtrlZ'),
          ),
          Text('lastAcceptedHotkey: $lastAcceptedHotkey'),
        ],
      ),
    );
  }
}

Fixed, please update to v0.1.7