Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tgalpha opened this issue Apr 28, 2022 · 1 comment
Closed

Comments

@tgalpha
Copy link

tgalpha commented Apr 28, 2022

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'),
        ],
      ),
    );
  }
}
@lijy91
Copy link
Member

lijy91 commented Apr 28, 2022

Fixed, please update to v0.1.7

@lijy91 lijy91 closed this as completed Apr 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants