Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 932
Jagan Akash Avatar asked Jan 09 '14 07:01

Jagan Akash


2 Answers

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.

like image 142
Gabriel Guarnieri Cardoso Avatar answered Nov 01 '22 16:11

Gabriel Guarnieri Cardoso


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

like image 34
zyc Avatar answered Nov 01 '22 17:11

zyc