Mousetrap.bind is not working when field is in focus?

6.1k Views Asked by At

I am using Mousetrap for create keyboard shortcuts, it was not working when any fields in focus. This is the link for view demo http://davidwalsh.name/keyboard-shortcuts from where i get the code. when i use to call

Mousetrap.bind('ctrl+m', function () {
    var button = $('[data-action="next-page"]');
    if (button.length) {
        button[0].click()
    }
});

like this it not working , when mouse points in text-box or drop-down etc,. Can any give solution for me. Thanks in advance.

2

There are 2 best solutions below

2
Gabriel Guarnieri Cardoso On

By default mousetrap disable the shortcuts when the focus is on input fields, dropdown, etc.. If your problem is with only one field, include the class mousetrap in it. If you want to disable all scenarios try the following code

Mousetrap.stopCallback = function () {
     return false;
}

This will overwrite the initial behavior and allow shortcuts in any field of the screen.

0
Cleverson Sacramento On

If you're using input or textarea or select elements, you must define the mousetrap class:

<textarea name="message" class="mousetrap"></textarea>

See the "Text fields" section in the oficial doc at https://craig.is/killing/mice