mousetrap
mousetrap copied to clipboard
overlapping binds
Hi,
I want to bind 'k' to one behaviour but 'k+j' and 'k+l' to another. If I keep the keys pressed, both keys trigger.
However, when I press 'k+j', 'k' is triggered as well. Is there any way to work around this? I wanted to only trigger the most specific binding
Yes, I think it can be corrected by changing the registering order of your bindings. Look at this issue #371
@Elvynia thanks for the reply
unfortunately, the work around described there did not work for me.
I was under the impression I could write combos such as Mousetrap.bind('k+j', func1); Mousetrap.bind('k', func2); Mousetrap.bind('j', func3);
seems I can only write combos that do not use modifiers such as cntrl or shift using Mousetrap.bind('k j', func1);
however, Mousetrap specifies that if I have a single key shortcut (k) and a multi key shortcut that starts with the same letter (k j) it can get confused because it can't tell if I'm doing a single shortcut or a combo.
This is not currently supported, and I’m not sure that it will be. My original thinking was that it would not really make sense to have a listener for g h
(g then h) but also for the g
key alone. Can you perhaps explain your use case in a little more detail?
I think the way you described about using the most specific binding is the way I would want it to work too, but the problem there is you would have to delay firing of the single key. For example, in the use case above with both g h
and g
bound, if you press g
there is no way that Mousetrap will know if this is pressed on its own or as part of the g h
sequence until the sequence times out (1 second goes by without pressing the h
key). That’s a pretty strange user experience because if you were to only put a g
binding it would fire immediately when you press the g
key, but when there is a competing sequence (more specific) it would have to wait.
This is explained somewhat in the documentation too: https://craig.is/killing/mice#api.bind.sequence
One other thing to note is that combinations only work when using modifier keys so k+j
in your original example would not work using mousetrap. See #101
@ccampbell I'm trying to reproduce some shortcuts my customers are used to. I agree with what you said and only realized it as I tried. I'll try to track the "state" of the command with a bind to k and a bind to j, assigning a different behavior depending on the state and I'll try to tell my experience here
We have a similar request here: a search area has a bunch of internal tabs for various search types, each triggered by "r something" (r = Recherche). And we'd like the whole search area toggle to be assigned to the simple "r" shortcut.
Technically, the assumption would be that if a sequence is underway but not finished, Mousetrap should fallback to see if it cannot match a shorter sequence. Sort of "greedy" behavior like regexes: try to match as much as possible...within a given time frame, but if it fails, fallback to shorter matches.
No support for this yet?