mousetrap icon indicating copy to clipboard operation
mousetrap copied to clipboard

Support for key classes and all keys capture

Open ghost opened this issue 11 years ago • 9 comments

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

ghost avatar Jun 15 '13 10:06 ghost

+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

mgray88 avatar Jun 22 '13 09:06 mgray88

When I am using mousetrap, I want all my keyboard handling chores to be done via mousetrap. That's why I want __all__ also.

ghost avatar Jun 22 '13 10:06 ghost

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

ccampbell avatar Jun 22 '13 17:06 ccampbell

+1 This would be really helpful!

adius avatar Nov 14 '13 09:11 adius

+1 I need that as well

marcelboettcher avatar Mar 16 '15 09:03 marcelboettcher

+1

roelvan avatar Feb 26 '16 12:02 roelvan

+1

EeshanKumar avatar Apr 13 '16 19:04 EeshanKumar

+1

bortexz avatar Aug 26 '16 16:08 bortexz

+1

beatlecz avatar Jul 02 '18 14:07 beatlecz