ccampbell / mousetrap

Simple library for handling keyboard shortcuts in Javascript
https://craig.is/killing/mice
Apache License 2.0
11.68k stars 968 forks source link

Multiple callbacks to the same sequence #304

Open vicary opened 9 years ago

vicary commented 9 years ago

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.

felixroos commented 9 years ago

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

lextiz commented 8 years ago

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.

robincafolla commented 8 years ago

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 ;) ).

vicary commented 8 years ago

@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:

louh commented 5 years ago

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