JQuery keyboard shortcuts ... not so global

Basically, I use Mr Resig's jQuery Hotkeys plugin to capture and process shortcuts like ctrl+o etc.

OK, maybe I don’t understand the concept, but I got the impression that ctrl+o running anywhere in the document would be captured by the document's hotkey handler.

For example, the following code works in general ...

 jQuery(document).bind('keydown', 'ctrl+o', fn); 

However, it fails if the user launches the hotkey when it is inside the input field.

It only works if I do the following:

 jQuery('body, input').bind('keydown', 'ctrl+o', fn); 

This is very bad for my health, since it is associated with binding the damn handler every time a new input field is added to the DOM. Even worse, I have no idea what to bind to in case of complex widgets like CodeMirror.

Don't know if my problem makes sense, maybe I'm using the wrong approach? I also tried to bind to the following objects, but that didn't work: window , document , body , div[contains the whole page]

NB: You can try it here .

+8
jquery dom events hotkeys codemirror
source share
2 answers

This is actually the intended functionality of the plugin:

 // Don't fire in text-accepting inputs that we didn't directly bind to if ( this !== event.target && (/textarea|select/i.test( event.target.nodeName ) || event.target.type === "text") ) { return; } 
+6
source share

Yes, JqueryHotkeys failed if the user launches a hotkey when inside the input field.

Alternatively, when I was browsing, I found shortcut.js , which provides similar functionality than jQuery-Hotkeys.

It is important . It also has the ability to enable or disable the "custom quick access function" when it is inside the input field.

+1
source share

All Articles