mousetrap icon indicating copy to clipboard operation
mousetrap copied to clipboard

Multiple callbacks to the same sequence

Open vicary opened this issue 8 years ago • 5 comments

I have some use cases, particularly chieffancypants/angular-hotkeys#186 which requires multiple callbacks under the same combo. Apart from key logging for A/B tests and general purpose event cancelling, there are many more possibilities opened up with this design.

An idea to allow this pattern while maintaining backward compatibility would be adding one more parameters in methods bind(keys, callback, action[, append]) and unbind(keys, action[, callback]).

This is more intuitive for those who is familiar with the event listener patterns, which I can remove a particular callback, while keeping backward compatible without the callback argument.

All we need to do is to skip this line mousetrap.js:845 when the append argument is truthy.

If it sounds all good I can make a PR.

vicary avatar Oct 12 '15 05:10 vicary

All we need to do is to skip this line mousetrap.js:845 when the append argument is truthy.

That did the trick for me. I needed a multipleSelect flag to be toggled true/false on CMD down/up. Would love to see that in the official repo

felixroos avatar Oct 13 '15 09:10 felixroos

Hello @vicary, as you can see this repository is not too actively maintained. If you have a ready PR could you please share it? In opposite case I could issue a PR. Could you please elaborate on how did you think to handle callback argument in unbind? It seems to me that having multiple handlers for one shortcut contradicts current implementation of unbind. BTW there is an (hopefully) active fork https://github.com/mousetrap-js/mousetrap.

lextiz avatar Feb 23 '16 21:02 lextiz

mousetrap-js/mousetrap is now back under maintenance. Although for the next couple weeks I'm short on time (new job, moving apartment). If you issue PRs against it, they will eventually be merged (or rejected ;) ).

robincafolla avatar Feb 24 '16 20:02 robincafolla

@lextiz In sequence callbacks, it was a single callback inside the object. Make it an array and expose to the context of unbind will do.

Don't quite have the time for PR in the upcoming weeks, it'd be great if you have the time to do it. :smile:

vicary avatar Mar 24 '16 18:03 vicary

I would love to see this added as either a feature or a plugin!

louh avatar Mar 30 '19 03:03 louh