_hyperscript icon indicating copy to clipboard operation
_hyperscript copied to clipboard

PROPOSAL: Enhance Keyboard Events

Open benpate opened this issue 4 years ago • 4 comments

There's an amazing javascript library called HotKeys that makes it stupid easy to build keyboard shortcuts. Hyperscript does this very well, but I think that we could make it even better by borrowing a couple of easy-to-implement ideas from Hotkeys.

We wouldn't need to change our event handlers, just enhance the events with some extra data that makes it easy for both humans and machines to recognize the keyboard combination being pressed:

on keypress(key)
    if key is "ctrl+s" return save() end
    if key is "ctrl+f" return find() end
    if key is "alt+q" return somethingElse() end
    if key is "ctrl+alt+M" return anotherCombo() end -- one way of handling uppercase characters.
    if key is "ctrl+alt+shift+m" return alt() end -- an alternate way of handling uppercase characters to consider.

This would really just require an extra function that looks at keyboard events, generates this string, then inserts it into the event details. Yes?

benpate avatar Apr 10 '21 17:04 benpate

I think we should make the is expression pluggable, like with the as expression, so this could be done as an extension.

1cg avatar Apr 15 '21 18:04 1cg

A pluggable is is an interesting idea I'd be interested in exploring further. But for this, it might be hard to implement. Perhaps this could just be a custom function like if codeFor(event) is "ctrl+s" -- this could be implemented outside of hyperscript.

benpate avatar Apr 16 '21 03:04 benpate

Perhaps this could just be a custom function like if codeFor(event) is "ctrl+s".

Or a custom conversion: if event as Hotkey is "Ctrl+S"

dz4k avatar Apr 21 '21 09:04 dz4k

Yes. This is the best way to handle this. It should be a custom conversion.

benpate avatar Apr 22 '21 16:04 benpate