is-hotkey icon indicating copy to clipboard operation
is-hotkey copied to clipboard

Can't match certain hotkeys on Mac

Open going-confetti opened this issue 3 years ago • 6 comments

First of all, thank you for this convenient library!

I faced an issue with one hotkey on Mac, which I believe is somewhat similar to https://github.com/ianstormtaylor/is-hotkey/issues/1

The hotkey is defined as mod+alt+n. It's not possible to match it on Mac with isHotkey/isKeyHotkey, because in the first case the which property has the value of 192 (while the expected value is 78), and in the second, the key prop has the value of "Dead" (isKeyHotkey expects "k").

Here's the keydown event object for cmd+opt+n:

altKey: true
charCode: 0
code: "KeyN"
composed: true
ctrlKey: false
key: "Dead"
keyCode: 192
metaKey: true
repeat: false
returnValue: true
shiftKey: false
which: 192

What would be the best way to fix this? Is checking the code prop safe in case key is "Dead"?

going-confetti avatar Mar 03 '21 12:03 going-confetti

Hmm interesting, do you know why that specific key results in a dead key? Maybe it needs to support which, key and code? All of which are separate.

ianstormtaylor avatar Mar 03 '21 17:03 ianstormtaylor

Unfortunately no. According to multiple resources, 192 is the code of backtick/grave accent ([1], [2]). opt+n inserts ˜ and results in a dead key as well (to make things even more confusing, which is 78 in this case)

This seems to depend on the layout - the issue is reproducible with British, U.S., and German but everything works as expected with Unicode Hex input, Arabic - QWERTY, and Russian - PC.

I'll investigate this further tomorrow.

going-confetti avatar Mar 03 '21 18:03 going-confetti

Hi there! I'm coming across the same issue formod+alt+n , is there any suggestion for a workaround or what a proposed fix might entail @going-confetti? Thank you for this helpful library @ianstormtaylor!

Jacfem avatar Sep 01 '22 18:09 Jacfem

Hi @Jacfem, I used a workaround for this specific hotkey. For mod+alt+n, instead of

if (isHotkey('mod+alt+n', e)) {
  // ...
}

you could use something like this

if (e.metaKey && e.altKey && e.code === 'KeyN') {
  // ...
}

@ianstormtaylor looks like checking the code property of the event object could indeed work for issues like this. Currently,isCodeHotkey is an alias for isHotkey, would you be open for a PR that will either

  1. introduce a breaking change by turning it into a function that checks code instead of which/key
  2. add a new exported function that will be doing that instead

?

going-confetti avatar Sep 02 '22 13:09 going-confetti

Thank you for the quick reply @going-confetti! That workaround seems to be working from a quick test, I really appreciate it.

Jacfem avatar Sep 02 '22 14:09 Jacfem

Thanks @going-confetti, I'd be down a PR there.

Is it possible that just checking code instead of which for the default case? Or do we need which sometimes and code sometimes? If possible to keep it simplest that would be great.

ianstormtaylor avatar Sep 19 '22 16:09 ianstormtaylor