react-hotkeys-hook icon indicating copy to clipboard operation
react-hotkeys-hook copied to clipboard

slash, backslash, and bracketright are not defined correctly in parseHotkeys.ts

Open luispied opened this issue 1 year ago • 9 comments

I found this bug that was giving me a hard time in my repo until I dig in into your code and found what was the issue.

Everytime I hit '-' it behaves as '/', I detected it as I have one hotkey for each of this keys, I tried a lot of different things in my repo and didn't get it fix.

https://github.com/JohannesKlauss/react-hotkeys-hook/blob/33c02ff7970639715b099a58e9eda69dd5e11eeb/src/parseHotkeys.ts#L10

You need to change - in favor of / that is the actual slash and not a hyphen

Also in the same file:

'#': 'backslash', // this should be '': 'backslash', '+': 'bracketright', // this should be ']': 'bracketright',

luispied avatar Jan 23 '24 16:01 luispied

I have a similar issue with the '-': 'slash' key where it doesn't trigger the expected behaviour as in earlier versions. Could you please take a look at this issue? Thanks

adam-vermes avatar Jan 29 '24 16:01 adam-vermes

This is a layout specific problem that gets addressed in version 5.

This has been fixed in pre release 5.0.0-1. Please try out this version. To listen to # use you just use useHotkeys('#', callback, {useKey: true})

Please let me know if that fixes your issue.

JohannesKlauss avatar Feb 09 '24 10:02 JohannesKlauss

Not the OP, but it fixes the "/" slash for me; thank you very much!

thediveo avatar Feb 11 '24 16:02 thediveo

Hi @JohannesKlauss I recently update to version 5.0.0-1 and tried to see if that fixed my issue, but it doesn't. I was using ignoreModifiers on the callback for the hook so when I tried to input / in different keyboard distribution it worked, but now it stoped working, so I read about the new option useKey that will probably helped me to achieve the same and sadly it does not. Idk if im doing something wrong but would be great if you can explain me how can I achieve this behavior with all the new changes in this new version.

To be clear and have a use case for you to reproduce it, I selected the key / to trigger an action, it works perfectly in U.S English distribution in Mac, but then I changed the distribution to for example Canadian French CSA and it doesn't worked, even sending useKey:true. I tried a combination between useKey and ignoreModifiers but same result.

luispied avatar Feb 21 '24 13:02 luispied

This is the way to use special characters that are different on different layouts:

https://codesandbox.io/p/sandbox/lucid-ioana-c8zjcj

Please let me know if that works for you.

JohannesKlauss avatar Feb 21 '24 14:02 JohannesKlauss

This is the way to use special characters that are different on different layouts:

https://codesandbox.io/p/sandbox/lucid-ioana-c8zjcj

Please let me know if that works for you.

@JohannesKlauss Is what I did, but even in your sandbox I see the same issue as I have.

Perhaps I didn't explain myself correctly, what I want is that no matter the distribution when I press they key in the english keyboard / I want to trigger the callback, so for french canadian the key in the same position of the keyboard is é.

As I said I could achieved this behavior in the past using ignoreModifiers but somehow perhaps useKey and ignoreModifiers cancel this functionality themselves.

luispied avatar Feb 21 '24 14:02 luispied

Ah I see. Actually I have to look into ignoreModifiers, good chance that this option gets removed in version 5. To achieve this you would listen to the code of the key, in this case Slash:

useHotkeys("Slash", () => setCount(count + 1));

This way, no matter the keyboard layout, it will always listen to the key which is the slash key in US layout.

JohannesKlauss avatar Feb 21 '24 15:02 JohannesKlauss

Ah I see. Actually I have to look into ignoreModifiers, good chance that this option gets removed in version 5. To achieve this you would listen to the code of the key, in this case Slash:

useHotkeys("Slash", () => setCount(count + 1));

This way, no matter the keyboard layout, it will always listen to the key which is the slash key in US layout.

I forgot about slash, I tried before.

The good news is that worked for me, so thank you so much @JohannesKlauss.

Can I suggest to add this to documentation as a use case, because I didn't see it there and would be very helpful for other devs.

Also I would want to suggest if it's possible to include the types for all the key strokes in the call of the hook, as useHotkeys(keys: KeysType[], callback, options). You have a link to another library that did that in the documentation, perhaps include it inside the library will improve the use of it and reduce the errors.

luispied avatar Feb 21 '24 16:02 luispied

Well version 5.0.0 is not officially out yet, I am currently overhauling the docs, but yes, this will be documented.

I am unsure about the typings, because the keys type can be a lot of different things.

JohannesKlauss avatar Feb 21 '24 16:02 JohannesKlauss