Reputation: 152
I add shortcuts to my web app, but I want to stop them when input is infocus...
$(function () {
$(document).keypress(function (event) {
var ch = String.fromCharCode(event.keyCode || event.charCode);
switch (ch) {
case 'p': case 'P':
mainMenuRegulations();
break;
case 's': case 'S':
mainMenuJurisprudence();
break;
case 'm': case 'M':
mainMenuModels()
break;
case 'i': case 'I':
showImprintContact();
break;
}
});
});
Upvotes: 0
Views: 251
Reputation: 318182
Just check if the currently active element (focused element) is an input inside the keypress event handler :
$(function () {
$(document).on('keypress', function(event) {
if (document.activeElement.tagName.toLowerCase() == 'input' ) {
var ch = String.fromCharCode(event.which);
switch (ch) {
// your code here
}
}
});
});
To be even more specific, you could check the document.activeElement
type as well, and check for text, number or whatever else you're using ?
Upvotes: 2