JS Gets the keyboard code and determines the key

Source: Internet
Author: User
Tags delete key switch loop

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.