gaucho-labs / leptos-hotkeys

a declarative way of using keyboard shortcuts + callbacks in leptos applications
https://leptos-hotkeys.vercel.app
MIT License
42 stars 8 forks source link

Raw Hotkeys #115

Open johnny-smitherson opened 2 months ago

johnny-smitherson commented 2 months ago

Hello

I'm trying to achieve this example https://github.com/gaucho-labs/leptos-hotkeys/blob/9143458a64eb5a5d738e6e977927e8f1b58dc71d/examples/ssr-demo/src/app.rs#L63

Which is capturing when the Ctrl or Shift key pressed by itself (not a part of a combination like Ctrl+S).

But the callback for controlleft controlright does not trigger like suggested in the example above (never logs that text to console). Tested both ff and chrome

I see this switch https://github.com/gaucho-labs/leptos-hotkeys/blob/9143458a64eb5a5d738e6e977927e8f1b58dc71d/leptos_hotkeys/src/hotkey.rs#L47

It seems the key is only pushed to the thing if it's not a modifier. This means we cannot capture events if only key pressed is modifier.

Which means we can't implement tetris: Shift and C are to hold. Ctrl and Z are to rotate 90° counterclockwise


But, say the switch above is changed to put the Ctrl both in the key codes and in the modifiers. That means we might have to match it using Ctrl+ControlLeft instead of just ControlLeft.

But then, when you throw the Shift key into the controls, and the user is mashing the keys in a desperate manner, a Ctrl keydown followed by Shift keydown (before the Ctrl keyup) might be seen as Ctrl+Shift+LeftShift, which will not match Shift+LeftShift

tl;dr Do you foresee any option to treat modifier keys as normal keys (to avoid the complications above)? Maybe separate use_raw_hotkeys! that gives the key codes without pre-processing the modifiers

johnny-smitherson commented 2 months ago

The macro defs are pretty gnarly maybe more of them is not a good idea

As workaround we commented out all mentions of Ctrl or Shift in leptos-hotkeys/leptos_hotkeys/src/hotkey.rs

friendlymatthew commented 2 months ago

Hi @johnny-smitherson sorry for the delay.

There isn't any specific reason why, when I was writing this I noticed a lot of other libraries followed such conventions.

Raw hotkeys sound cool and doable. Best of luck with Tetris! Would love to see it at the end :)