RobertWHurst / Keystrokes

Keystrokes as an easy to use library for binding functions to keys and key combos. It can be used with any TypeScript or JavaScript project, even in non-browser environments.
MIT License
153 stars 6 forks source link

Odd behaviour with Control/Command key on Mac #21

Closed silverwind closed 1 year ago

silverwind commented 1 year ago

Describe the bug

There seems to be some odd behaviour around the Command and Control keys on Mac:

To Reproduce

  1. On Mac, open https://codesandbox.io/s/quizzical-nightingale-c6t37s
  2. Input control+s, it's logged correctly
  3. Input meta+s (Command+s), it's logged correctly
  4. Input control+s, it's logged as meta+s and subsequent presses no longer log at all
{"keyCombo":"control+s"} // ok
{"keyCombo":"meta+s"} // ok
{"keyCombo":"meta+s"} // not what I pressed

Expected behavior These combos to be detected the same way they are entered.

silverwind commented 1 year ago

This snippet of mousetrap may be helpful.

Here is the same demo with mousetrap, it does not exhibit this issue:

https://codesandbox.io/s/bitter-cache-22rssh?file=/src/index.js

RobertWHurst commented 1 year ago

I believe the offending file is browser-bindings.ts, likely to do with the special handling of the Command key. I can't debug this until Friday because I'm away from any mac devices, but will have a look as soon as I can. Thanks for filing the issue @silverwind, it's greatly appreciated.

RobertWHurst commented 1 year ago

@silverwind Just published v1.2.1. You should now be able to bind meta/command as you expect, as well as use preventDefault as you expect. Let me know if you run into any more issues. Thanks for raising this issue, it's an important one. 🍻

silverwind commented 1 year ago

Thanks, I updated the dependency in https://codesandbox.io/s/gracious-fog-xswpdd?file=/src/index.js, and now it's working as expected and both cmd+s and ctrl+s log as expected.