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
158 stars 6 forks source link

Create bindings using KeyboardEvent#code value #33

Closed vladdekhanov closed 11 months ago

vladdekhanov commented 11 months ago

Describe the bug Library doesn't support code or which property to make hotkey language agnostic. It compare event key by value due to works with only one keyboard layout.

To Reproduce Imagine you have keyboard with 2 layouts: native language and english (i.e. French and English).

Steps to reproduce the behavior:

  1. Bind event
    
    import { bindKey } from '@rwh/keystrokes'

bindKey('H', { onPressed: () => { ... }, onReleased: () => { ... }, });


2. Press H button in English layout. It works
3. Change active keybord layout to French. Press physically same button. It doesn't work what is obviously is not clear for any user.

**Expected behavior**
Library should work with `code` property (physical id of the button) and not with a `key` (it contains value, changable thing), otherwise library would be used only for few countries where people use english as a native language.
RobertWHurst commented 11 months ago

I was under the impression that typically keyboard shortcuts are often standardized by key name rather than physical location on the keyboard.

I checked both Mac and Windows via these support articles as an example.

Changing the locale does not change these shortcuts. This is more relevant for application shortcuts. But I can see the issue for something like a game. Is that what you are working on?

Keystrokes uses key specifically so it is locale independent, so this is actually desired. Perhaps we need some sort of key code syntax though to allow you to choose.

RobertWHurst commented 11 months ago

@vladdekhanov alright, I've added the ability to bind code names in bindings and combos. All you need to do is prefix the name with @. For example, binding the keys in the position of the QUERTY keyboard's A and X key can be done with the combo @KeyA + @KeyX. Let me know if you have any feedback.

See the 1.5.0 release to try it out.

Thanks for raising this one @vladdekhanov

vladdekhanov commented 11 months ago

Thanks a lot and very appreciate your quick response. I've got a flu and wasn't available to response. Thank you for solution with @ symbol. It definitely works in most cases. Looking forward to try it out and wish you a merry Christmas.