jQuery/Javascript - 拒绝keyDown事件上的"控制"键

con*_*att 5 javascript jquery javascript-events keydown

什么是干净的在JavaScript或jQuery的方式来过滤掉控制键.通过控制键,我指的是任何不是AZ,0-9或特殊字符的键(即!,@,#等).我只想过滤出"Shift","Alt",F1-F9,Caps Lock等键.

我确信我可以从事件参数中检查每个ASCII码...但我想知道是否有"更清洁"的解决方案.

注意:我正在开发专门针对IE 8的应用程序

con*_*att 5

我带着这样的东西:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)