JQuery / Javascript - Undo "control" keys in keyDown event

What is cleanest in JavaScript or jQuery for filtering control keys. By control keys, I mean any key that is NOT AZ, 0-9, or special characters (that is, !, @, # Etc.). I just want to filter out keys like "Shift", "Alt", F1-F9, Caps Lock, etc.

I'm sure I can check every ASCII code from the event argument ... but I'm wondering if there is a “cleaner” solution.

Note. I am developing an application specifically for IE 8

+5
source share
3 answers

- :

function (e, inputElement) {
    // If the user gives the textbox any keyboard input, mark the input box as "dirty"
    var scope = this;
    var k = e.which;

    // Verify that the key entered is not a special key
    if (k == 20 /* Caps lock */
     || k == 16 /* Shift */
     || k == 9 /* Tab */
     || k == 27 /* Escape Key */
     || k == 17 /* Control Key */
     || k == 91 /* Windows Command Key */
     || k == 19 /* Pause Break */
     || k == 18 /* Alt Key */
     || k == 93 /* Right Click Point Key */
     || ( k >= 35 && k <= 40 ) /* Home, End, Arrow Keys */
     || k == 45 /* Insert Key */
     || ( k >= 33 && k <= 34 ) /*Page Down, Page Up */
     || (k >= 112 && k <= 123) /* F1 - F12 */
     || (k >= 144 && k <= 145 )) { /* Num Lock, Scroll Lock */
        return false;
    }
    else {
        scope.setPointValueDirtyStatus(inputElement, true);
    }
}
+5

event.which - . 17, - 16, @- , 16 - 50. , , , .

+3

a-z ( 65 - 90), 0-9 (48 - 57). , shift , .

$("...").keydown(function(ev){
    var k = ev.which;
    if(!(k >= 65 && k <= 90) /* a-z */
    || !(k >= 48 && k <= 57) /* numbers */
    || !(k >= 96 && k <= 111) /* numeric keyboard*/
    || k != 59 || k != 61 || k != 188 || k != 190 || k != 191 || k != 191
    || k != 192 || !(k >= 219 && k <= 222) || k != 32 /* Comma's,  etc. */
    || ev.ctrlKey || ev.altKey/* || ev.shiftKey*/){
        //Filter
    }
})
+1

All Articles