mousetrap
mousetrap copied to clipboard
Mousetrap.trigger(key) throws an error
Hi,
I am having issues with bindings, which use '+' or '-' as a standalone key or in sequence.
Case in point: I have buttons, which has '+' and '-' assigned to them ('plus' and 'minus'). I also have three more sets of buttons which use sequences (ie. g +, g -, b +, b -, c +, c -). When the cursor focus is on the document ('body'), all of them trigger just fine. However, there is one input field where those characters are not allowed, and I need to be able to press '+' and trigger it from within the input field (losing focus is not an issue).
API says, I can use Mousetrap.trigger(key) for this very scenario. Unfortunately, when cursor is within the field and I get to fire the trigger method, I get
Uncaught TypeError: Cannot read property 'className' of undefined > mousetrap.js:966
When debugging the behavior I get to the point, when Mousetrap tries to execute a combo (it is always the first one, 'g +' or 'g -' – whatever is pressed, '+' or '-') and fails to do it anyway.
I seemed to have found a workaround for a '+' situation: I would use '+' for the button and 'g plus' for the other shortcut, but the issue persists with '-' (adding 'minus' alias doesn't seem to work for me).
Here is the piece of my code where it gets hairy
fireKey: function (key, e) {
if (e && $(e.target).is('input')) {
e.preventDefault();
e.stopPropagation();
$(e.target).blur();
this.fireKey(key);
return;
}
if ($.inArray(key, this.opts.existingKeys) >= 0) {
Mousetrap.trigger(key);
}
}
whereas this.opts.existingKeys
is a plain array with all the keys bound to Mousetrap.
Any ideas?
Also got this. Seems that mousetrap is trying to clear keycombos. It tries to clear in the context of the element, which is not available in .trigger()