Disable keyboard in some fields

5

I need to make the letter f only work in text fields, in other places it does not work, eg:

$(document).keyup(function(e) {
    if (e.keyCode == 37) { 
               return false;
     }
});
    
asked by anonymous 20.04.2014 / 03:46

1 answer

5

For this, you only need to prevent propagation from the event in the text fields - so that the generic code does not act on it. In addition, it is best to treat keydown instead of keyup because - when keyup is activated - the effect of pressing the key has already happened.

$(document).keydown(function(e) {
    if (e.keyCode == 70) { 
        return false; // Equivalente a: e.stopPropagation(); e.preventDefault();
    }
});

$('input[type="text"]').keydown(function(e) {
    e.stopPropagation();
});

Example in jsFiddle .

    
20.04.2014 / 03:57