This article involves syntax:
// when the keyboard presses // when the keyboard is loosened // when the key is pressed and a key is released // get Keyboard Code // can accept a Unicode value that can be specified, returning a string
The code is as follows:
<!doctype html>"Zh-en">"Utf-8"/> <meta name="Viewport"Content="width=device-width, initial-scale=1, maximum-scale=1, User-scalable=no"> <meta http-equiv="x-ua-compatible"Content="Ie=edge"> <title> native JavaScript keyboard events </title> <link rel="icon"href="Http://tools.renpengpeng.com/favicon.ico"/> <style> *{padding:0; Padding-top:10px; Margin:0; Text-Align:center; } #jp, #jptxt {margin-top:30px; width:500px; heihgt:300px; Margin:0Auto; } </style>"JP"> Press the keyboard </div><div id="Jptxt"></div><script>//get keyboard Press and activate functionDocument.onkeydown =function (even) {//get Keyboard Code varJpcode =Even.keycode; //Get the letter of the letter pad on the keyboard varKeychar=String.fromCharCode (Jpcode); alert (Keychar); //declare the ID on the page to the variable varJpcodetxt = document.getElementById ('Jptxt') //Print the keyboard code on the pagedocument.getElementById ('JP'). InnerHTML =Jpcode; //start Switch Loop * * For Chinese hints Switch(jpcode) { Case 8: jpcodetxt.innerhtml="you pressed the Delete key (Backspace)"; Break; Case -: jpcodetxt.innerhtml="you pressed the OK key (enter)"; Break; Case -: jpcodetxt.innerhtml="you pressed alt."; Break; default: jpcodetxt.innerhtml="The ghost knows what you're pressing."; Break; }} </script></body>Transferred from: 78120922?locationnum=8&fps=1
JS Gets the keyboard code and determines the key