mousetrap
mousetrap copied to clipboard
Support for key classes and all keys capture
I want to do things like this...
Mousetrap.bind('__alpha__', handleAllAlphaKeys);
Mousetrap.bind('ctrl+ __num__', handleAllNumberKeys);
// other classes
Let's say I want to save some content to the server as the user types it. I trigger a timeout on each keypress and debounce it until some delay after which I take the content and send it to the server. In such cases, I need to catch ALL the keys. There is no way to do it via Mousetrap currently.
I want to do this:
Mousetrap.bind('__all__', handleAllKeys);
+1 for the __alpha__
and __num__
but as for __all__
, wouldn't it be easier just to bind to the keyup
event in vanilla javascript (or jquery if that's your cup of tea)? that would catch all the keys. Or if you wanted to ignore things like the shift, enter, page up, page down, etc. use keypress
which only catches printable characters
When I am using mousetrap, I want all my keyboard handling chores to be done via mousetrap. That's why I want __all__
also.
@greengit one thing you might be able to do is override Mousetrap.handleKey
. It is not documented but it is called for every key event and passed 3 arguments function handleKey(character, modifiers, event)
.
(function(Mousetrap) {
var originalHandleKey = Mousetrap.handleKey;
Mousetrap.handleKey = function(character, modifiers, e) {
// do whatever you want here
// then call original
return originalHandleKey(character, modifiers, e);
};
} (Mousetrap))
I haven't tested this, but it might get you on the right track.
+1 This would be really helpful!
+1 I need that as well
+1
+1
+1
+1