将密钥代码转换为代表密钥的字符串的最简单方法?

时间:2011-11-27 23:26:55

标签: javascript keypress

我正在使用以下事件监听器来捕获按键:

window.addEventListener("keydown", function(event) {} );

获取表示按下键的字符串的最简单方法是什么,比如33的值将是“Pg Up”,84将是“F5”,40将是“向下箭头”等...是否存在一些有用的javascript库吗?

1 个答案:

答案 0 :(得分:4)

这个网站有一个清单。看起来很容易将它转换为一组常量或将其放入数组中。

http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

代码适用于美国键盘。事实上,如果你只是偷了他们的js ......

 var keyPressed;
 if (charCode == 8) keyPressed = "backspace"; //  backspace
 if (charCode == 9) keyPressed = "tab"; //  tab
 if (charCode == 13) keyPressed = "enter"; //  enter
 if (charCode == 16) keyPressed = "shift"; //  shift
 if (charCode == 17) keyPressed = "ctrl"; //  ctrl
 if (charCode == 18) keyPressed = "alt"; //  alt
 if (charCode == 19) keyPressed = "pause/break"; //  pause/break
 if (charCode == 20) keyPressed = "caps lock"; //  caps lock
 if (charCode == 27) keyPressed = "escape"; //  escape
 if (charCode == 33) keyPressed = "page up"; // page up, to avoid displaying alternate character and confusing people
 if (charCode == 34) keyPressed = "page down"; // page down
 if (charCode == 35) keyPressed = "end"; // end
 if (charCode == 36) keyPressed = "home"; // home
 if (charCode == 37) keyPressed = "left arrow"; // left arrow
 if (charCode == 38) keyPressed = "up arrow"; // up arrow
 if (charCode == 39) keyPressed = "right arrow"; // right arrow
 if (charCode == 40) keyPressed = "down arrow"; // down arrow
 if (charCode == 45) keyPressed = "insert"; // insert
 if (charCode == 46) keyPressed = "delete"; // delete
 if (charCode == 91) keyPressed = "left window"; // left window
 if (charCode == 92) keyPressed = "right window"; // right window
 if (charCode == 93) keyPressed = "select key"; // select key
 if (charCode == 96) keyPressed = "numpad 0"; // numpad 0
 if (charCode == 97) keyPressed = "numpad 1"; // numpad 1
 if (charCode == 98) keyPressed = "numpad 2"; // numpad 2
 if (charCode == 99) keyPressed = "numpad 3"; // numpad 3
 if (charCode == 100) keyPressed = "numpad 4"; // numpad 4
 if (charCode == 101) keyPressed = "numpad 5"; // numpad 5
 if (charCode == 102) keyPressed = "numpad 6"; // numpad 6
 if (charCode == 103) keyPressed = "numpad 7"; // numpad 7
 if (charCode == 104) keyPressed = "numpad 8"; // numpad 8
 if (charCode == 105) keyPressed = "numpad 9"; // numpad 9
 if (charCode == 106) keyPressed = "multiply"; // multiply
 if (charCode == 107) keyPressed = "add"; // add
 if (charCode == 109) keyPressed = "subtract"; // subtract
 if (charCode == 110) keyPressed = "decimal point"; // decimal point
 if (charCode == 111) keyPressed = "divide"; // divide
 if (charCode == 112) keyPressed = "F1"; // F1
 if (charCode == 113) keyPressed = "F2"; // F2
 if (charCode == 114) keyPressed = "F3"; // F3
 if (charCode == 115) keyPressed = "F4"; // F4
 if (charCode == 116) keyPressed = "F5"; // F5
 if (charCode == 117) keyPressed = "F6"; // F6
 if (charCode == 118) keyPressed = "F7"; // F7
 if (charCode == 119) keyPressed = "F8"; // F8
 if (charCode == 120) keyPressed = "F9"; // F9
 if (charCode == 121) keyPressed = "F10"; // F10
 if (charCode == 122) keyPressed = "F11"; // F11
 if (charCode == 123) keyPressed = "F12"; // F12
 if (charCode == 144) keyPressed = "num lock"; // num lock
 if (charCode == 145) keyPressed = "scroll lock"; // scroll lock
 if (charCode == 186) keyPressed = ";"; // semi-colon
 if (charCode == 187) keyPressed = "="; // equal-sign
 if (charCode == 188) keyPressed = ","; // comma
 if (charCode == 189) keyPressed = "-"; // dash
 if (charCode == 190) keyPressed = "."; // period
 if (charCode == 191) keyPressed = "/"; // forward slash
 if (charCode == 192) keyPressed = "`"; // grave accent
 if (charCode == 219) keyPressed = "["; // open bracket
 if (charCode == 220) keyPressed = "\\"; // back slash
 if (charCode == 221) keyPressed = "]"; // close bracket
 if (charCode == 222) keyPressed = "'"; // single quote  var lblCharCode = getObject('spnCode');