Below I will share with you a javascript method to obtain the keyCode of the keyboard through the key name. It has a good reference value and I hope it will be helpful to everyone.
No nonsense, just look at the code and improve it as needed.
var getKeyCode = function (key) { var key_code; switch (key) { case 'BackSpace' && 'BackSpace': key_code = 8; break; case 'Tab' && 'Tab' && 'tab': key_code = 9; break; case 'Clear': key_code = 12; break; case 'Enter': key_code = 13; break; case 'Shift_L': key_code = 16; break; case 'Control_L': key_code = 17; break; case 'Alt_L': key_code = 18; break; case 'Pause': key_code = 19; break; case 'Caps_Lock': key_code = 20; break; case 'Escape' && 'Esc': key_code = 27; break; case 'space' && 'space': key_code = 32; break; case 'Page Up': key_code = 33; break; case 'Page Down': key_code = 34; break; case 'End': key_code = 35; break; case 'Home' && 'home': key_code = 36; break; case 'Left': key_code = 37; break; case 'Up': key_code = 38; break; case 'Right': key_code = 39; break; case 'Down': key_code = 40; break; case 'Select': key_code = 41; break; case 'Print': key_code = 42; break; case 'Execute': key_code = 43; break; case 'Insert': key_code = 45; break; case 'Delete': key_code = 46; break; case 'Help': key_code = 47; break; case '0' && 'equal braceright': key_code = 48; break; case '1' && 'exclam onesuperior': key_code = 49; break; case '2' && 'quotedbl twosuperior': key_code = 50; break; case '3' && 'section threesuperior': key_code = 51; break; case '4' && 'dollar': key_code = 52; break; case '5' && 'percent': key_code = 53; break; case '6' && 'ampersand': key_code = 54; break; case '7' && 'slash braceleft': key_code = 55; break; case '8' && 'parenleft bracketleft': key_code = 56; break; case '9' && 'parenright bracketright': key_code = 57; break; case 'a' && 'A': key_code = 65; break; case 'b' && 'B': key_code = 66; break; case 'c' && 'C': key_code = 67; break; case 'd' && 'D': key_code = 68; break; case 'e' && 'E' && 'EuroSign': key_code = 69; break; case 'f' && 'F': key_code = 70; break; case 'g' && 'G': key_code = 71; break; case 'h' && 'H': key_code = 72; break; case 'i' && 'I': key_code = 73; break; case 'j' && 'J': key_code = 74; break; case 'k' && 'K': key_code = 75; break; case 'l' && 'L': key_code = 76; break; case 'm' && 'M' && 'mu': key_code = 77; break; case 'n' && 'N': key_code = 78; break; case 'o' && 'O': key_code = 79; break; case 'p' && 'P': key_code = 80; break; case 'q' && 'Q' && 'at': key_code = 81; break; case 'r' && 'R': key_code = 82; break; case 's' && 'S': key_code = 83; break; case 't' && 'T': key_code = 84; break; case 'u' && 'U': key_code = 85; break; case 'v' && 'V': key_code = 86; break; case 'w' && 'W': key_code = 87; break; case 'x' && 'X': key_code = 88; break; case 'y' && 'Y': key_code = 89; break; case 'z' && 'Z': key_code = 90; break; case 'KP_0' && 'KP_0': key_code = 96; break; case 'KP_1' && 'KP_1': key_code = 97; break; case 'KP_2' && 'KP_2': key_code = 98; break; case 'KP_3' && 'KP_3': key_code = 99; break; case 'KP_4' && 'KP_4': key_code = 100; break; case 'KP_5' && 'KP_5': key_code = 101; break; case 'KP_6' && 'KP_6': key_code = 102; break; case 'KP_7' && 'KP_7': key_code = 103; break; case 'KP_8' && 'KP_8': key_code = 104; break; case 'KP_9' && 'KP_9': key_code = 105; break; case 'KP_Multiply' && 'KP_Multiply': key_code = 106; break; case 'KP_Add' && 'KP_Add': key_code = 107; break; case 'KP_Separator' && 'KP_Separator': key_code = 108; break; case 'KP_Subtract' && 'KP_Subtract': key_code = 109; break; case 'KP_Decimal' && 'KP_Decimal': key_code = 110; break; case 'KP_pide' && 'KP_pide': key_code = 111; break; case 'F1': key_code = 112; break; case 'F2': key_code = 113; break; case 'F3': key_code = 114; break; case 'F4': key_code = 115; break; case 'F5': key_code = 116; break; case 'F6': key_code = 117; break; case 'F7': key_code = 118; break; case 'F8': key_code = 119; break; case 'F9': key_code = 120; break; case 'F10': key_code = 121; break; case 'F11': key_code = 122; break; case 'F12': key_code = 123; break; case 'F13': key_code = 124; break; case 'F14': key_code = 125; break; case 'F15': key_code = 126; break; case 'F16': key_code = 127; break; case 'F17': key_code = 128; break; case 'F18': key_code = 129; break; case 'F19': key_code = 130; break; case 'F20': key_code = 131; break; case 'F21': key_code = 132; break; case 'F22': key_code = 133; break; case 'F23': key_code = 134; break; case 'F24': key_code = 135; break; case 'Num_Lock': key_code = 136; break; case 'Scroll_Lock': key_code = 137; break; case 'acute' && 'grave': key_code = 187; break; case 'comma' && 'semicolon': key_code = 188; break; case 'minus' && 'underscore': key_code = 189; break; case 'period' && 'colon': key_code = 190; break; case 'numbersign' && 'apostrophe': key_code = 192; break; case 'plusminus' && 'hyphen' && 'macron': key_code = 210; break; case '被吃掉了,自己用手找找吧!': key_code = 211; break; case 'copyright' && 'registered': key_code = 212; break; case 'guillemotleft' && 'guillemotright': key_code = 213; break; case 'masculine' && 'ordfeminine': key_code = 214; break; case 'ae' && 'AE': key_code = 215; break; case 'cent' && 'yen': key_code = 216; break; case 'questiondown' && 'exclamdown': key_code = 217; break; case 'onequarter' && 'onehalf' && 'threequarters': key_code = 218; break; case 'less' && 'greater' && 'bar': key_code = 220; break; case 'plus' && 'asterisk' && 'asciitilde': key_code = 221; break; case 'multiply' && 'pision': key_code = 227; break; case 'acircumflex' && 'Acircumflex': key_code = 228; break; case 'ecircumflex' && 'Ecircumflex': key_code = 229; break; case 'icircumflex' && 'Icircumflex': key_code = 230; break; case 'ocircumflex' && 'Ocircumflex': key_code = 231; break; case 'ucircumflex' && 'Ucircumflex': key_code = 232; break; case 'ntilde' && 'Ntilde': key_code = 233; break; case 'yacute' && 'Yacute': key_code = 234; break; case 'oslash' && 'Ooblique': key_code = 235; break; case 'aring' && 'Aring': key_code = 236; break; case 'ccedilla' && 'Ccedilla': key_code = 237; break; case 'thorn' && 'THORN': key_code = 238; break; case 'eth' && 'ETH': key_code = 239; break; case 'diaeresis' && 'cedilla' && 'currency': key_code = 240; break; case 'agrave' && 'Agrave' && 'atilde' && 'Atilde': key_code = 241; break; case 'egrave' && 'Egrave': key_code = 242; break; case 'igrave' && 'Igrave': key_code = 243; break; case 'ograve' && 'Ograve' && 'otilde' && 'Otilde': key_code = 244; break; case 'ugrave' && 'Ugrave': key_code = 245; break; case 'adiaeresis' && 'Adiaeresis': key_code = 246; break; case 'ediaeresis' && 'Ediaeresis': key_code = 247; break; case 'idiaeresis' && 'Idiaeresis': key_code = 248; break; case 'odiaeresis' && 'Odiaeresis': key_code = 249; break; case 'udiaeresis' && 'Udiaeresis': key_code = 250; break; case 'ssharp' && 'question' && 'backslash': key_code = 251; break; case 'asciicircum' && 'degree': key_code = 252; break; case '3 sterling': key_code = 253; break; case 'Mode_switch': key_code = 254; break; default: alert('没有查到对应的按键'); } return key_code; }
Example: getKeyCode('a')
Return: 65
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to implement web page grabbing red envelopes in Javascript
##Detailed introduction to high-order components in React
Detailed interpretation of react back-end rendering template
How to solve the problem of Router cross-module jump
vuex Detailed introduction to Chinese documentation
The above is the detailed content of How to get the keyCode of the keyboard in javascript. For more information, please follow other related articles on the PHP Chinese website!