Reputation: 1803
I am using the following event listener to capture keypresses:
window.addEventListener("keydown", function(event) {} );
What is the easiest way to get a string which represents the key pressed, like the value of 33 would be "Pg Up", 84 would be "F5", 40 would be "Down Arrow" etc... Is there some useful javascript library for this?
Upvotes: 4
Views: 4282
Reputation: 39902
This website has a list. It seems it would be pretty easy to convert it to a set of constants or place it into an array.
http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
Codes are for US keyboards. In fact, if you just steal their 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');
Upvotes: 4