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.

Filters seem scoped, but aren't

See original GitHub issue

An example:

useHotkeys('Delete', () => {
    // do something
}, { filter: ignoreInputElements });

useHotkeys('Ctrl+Shift+Delete', () => {
    // do something
}, { filter: () => true });

You might want to have different filters for different shortcuts. Here, for example, ‘Delete’ is something you might press during input, so needs to be filtered to ignore input elements, but ‘Ctrl+Shift+Delete’ is not.

Unfortunately, the hotkey filter functionality is global, so the last filter that’s set always wins. In the above example, ‘Delete’ will fire everywhere, even in input elements, which is not what the author intended.

This isn’t very react-y/declarative, as different render orders/rerenderings elsewhere can produce all sorts of strange behaviours here. It’d be better to either scope the filters to the specific shortcut somehow, or to make this behaviour obvious in the useHotkeys API. Setting the filter via useHotkeys.filter = ... would make the fact that it’s global much clearer, for example.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
JohannesKlausscommented, Dec 23, 2020

I implemented a custom filter logic.

Basically everything works as before, but the filter is now scoped to each hook instance instead of globally.

Since this is a behavioral breaking change, I bumped the major version to 3.0.0

1reaction
SteffeyDevcommented, Dec 7, 2020

I found a fairly easy fix that I implemented in my fork (https://github.com/SteffeyDev/react-hotkeys-hook/blob/feature/scope/src/useHotkeys.ts). Can’t PR though cause I made a lot of other changes in my fork that ended up not being good because of how poorly hotkeys-js handles scoping.

The idea is that you set the global hotkeys.filter to () => true so that all events are allowed through. Then you do all the filtering in the hook. The default filtering that hotkeys uses is fairly simple to copy over if the user does not pass a filter in the hook, but if they do you can use their filter instead. Once again, see the version in my fork to see it implemented in a working fashion (just ignore all my scope changes that don’t work well).

If someone wants to fork and implement, it should be pretty easy. Unfortunately this would be a breaking change so would probably need a new major version.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Proper Way to Use Scoped Filters With Job Consumers #3672
I am trying to use Scoped filters to do this. ... There are no middleware filters for job consumers, and there aren't any...
Read more >
Scoped filtering over column range with & logic - Stack Overflow
I'm trying to find a method of filtering over a specific range of columns in a data frame to filter to rows that...
Read more >
Filters in ASP.NET Core | Microsoft Learn
In this article. How filters work; Implementation; Filter scopes and order of execution; Cancellation and short-circuiting; Dependency injection ...
Read more >
Do not see Filter by Scope option under filters in report for ...
When we apply scoping rules on the report we see the filter by scope option for Account,Opportunities and Financial Accounts which will show...
Read more >
Scripted Filters in Scoped Applications - ServiceNow Community
To do this, I thought to set-up a scripted filter using a Script Include. Which looks like ... But when I put javascript:new...
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