question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Proposal] Hotkeys

See original GitHub issue

Summary

Very often user interfaces provide useful hotkeys in order to facilitate or speed up user interactions and navigation. Most of the time these hotkeys are global, meaning that the event associated with them is added to and handled by the document object (or at least the outermost wrapping element). While the implementation for such a global hotkey is not hard, providing it as a simple utility hook might turn out to be useful.

Hotkey is a key or combination of keys providing a quick access to a function within the application.

Draft

The hotkey object would look something like this:

interface Hotkey {
  key: string
  code: string
  keyCode: number
  shift: boolean
  alt: boolean
  ctrl: boolean
  meta: boolean
  enabled: boolean
  allowInsideInputs: boolean
}
  • The key, code and keyCode properties represent the key itself (same as the KeyboardEvent)
  • The ctrl, shift, alt and meta properties are the modifier keys. True is a modifier key is required for the hotkey handle to run, False if it is not required and undefined/null (left out) if modifier key is irrelevant.
  • The enabled property shows whether the hotkey is currently enabled/available and its handle can be run.
  • The allowInsideInputs indicates whether the hotkey can trigger on an input element (for example while the user is typing something). A simple check is done by examining the target element’s instance type and checking for contenteditable attribute, though this is not perfect and will require some thinking.

Then a handle function and a variable number of hotkey objects are passed to a hook

function useHotkey(handle, ...hotkeys)

which then adds an event listener to the document object. Whenever a key is pressed it is checked whether the hotkey is pressed and the handle is called if so. This way not only a hotkey can be defined seamlessly, but also only be available only when a given component (tree) is rendered, or perhaps if user has a certain permission, or even something that I can’t think of right now.

I think that this feature would be nice to have, since the aim is to make this project a general use UI library. Tell what you guys think… I have something really close implemented in a project of mine, so if this sparks interest, then I could work on it in the near future.


  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:11
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
hearchadcommented, Dec 6, 2020

@oliviertassinari I was going to propose HotKeys and found this thread. Can I re-open? My approach is not a hook, but a wrapper component that enables the consumer to either accept the default click simulation or write their own handler. I wrote about it here. https://dev.to/chadsteele/how-to-add-hotkeys-in-react-4610

simple

<HotKey keys="ArrowUp">
    <SomeClickableComponent />
</HotKey>

optional

<HotKey keys="Enter Space" scope={window.document} callback={myfunc} tooltip="Hit Enter or Spacebar">
    <SomeClickableComponent />
</HotKey>

p.s. I disagree with earlier comments. UI library should support input as well as output and obviously somebody agrees. 😃 https://material.io/design/interaction/gestures.html#principles

1reaction
oliviertassinaricommented, Aug 3, 2019

@muqg I have added the waiting for users upvotes tag. I’m closing the issue as we are not sure people are looking for such abstraction. So please upvote this issue if you are. We will prioritize our effort based on the number of upvotes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Book Proposal Shortcut for Busy Scholars
The Book Proposal Shortcut is for scholarly authors who are ready to do the work of writing a book proposal—and want to be...
Read more >
Proposed Single Key Shortcut Alternative
This will allow speech users to make full use of programs that offer single key shortcuts to keyboard users.
Read more >
Keyboard shortcuts
These shortcuts can enhance your productivity by minimizing the occurrence of incorrect mouse clicks and speeding up data ... Proposal breakdown structure ...
Read more >
[Proposal] Hotkeys · Issue #15873 · mui/material-ui
Hotkey is a key or combination of keys providing a quick access to a function within the application.
Read more >
Proposal Binding Particular Parameters of Effects to Hot-Keys
This page is a proposal to enable the binding of particular parameters of effects to buttons or keyboard shortcuts. Proposal pages are used ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found