JS user presses the keyboard, displays the KeyCode instance

Source: Internet
Author: User

JS Instance

The code is as follows Copy Code
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> the user presses the keyboard to display keycode</title>
<style>
body{
Text-align:center;
font:30px/1.5 5fae8f6f96c59ed1, Arial;
}
pre{color:green;padding:10px 15px;background: #f0f0f0; border:1px dotted #333; font:12px/1.5 Courier new;text-align: Left;}
Span{color: #999;}
</style>
<script type= "Text/javascript" >
Window.onload = function ()
{
var OP = document.getElementsByTagName ("P") [0];
Document.onkeydown = function (event)
{
var event = Event | | window.event;
op.innerhtml = Event.keycode;
return False
}
}
</script>
<body>
<p> here will show you press the key keycode</p>
</body>

Of course, we can also use jquery Event.which to get the user to press the keyboard value

Grammar
Event.which parameter Description
Event required. Specify the events to check. This event parameter comes from the event-binding function.

jquery discards the standard button attribute using which, which is a bit confusing.

Which is introduced by Firefox, IE does not support. Which is meant to get the keyboard's key value (KeyCode).

The which in jquery can be either the key value of the keyboard or the key value of the mouse.


That is, when the user to determine which button the keyboard can be used which, when the user to determine which button to press the mouse can also use which. It's dual.

  code is as follows copy code
//Add which for Key Events
if (Event.which = null && (event.charcode!= null | | event.keycode!= null)) {
Event.which = Event.charcode!= null? Event.charCode:event.keyCode;
}
//Add which for click:1 = = left; 2 = = middle 3 = = right
//Note:button are not normalized, so don ' t u Se it
if (!event.which && event.button!== undefined) {
Event.which = (Event.button & 1? 1: (Even T.button & 2? 3: (Event.button & 4? 2:0));
}

The standard button uses 0,1,2 to represent the left, middle, and right keys of the mouse. The Which of jquery is used with 1,2,3.
It's also a bit of a bummer that the jquery document Event.which does not mention that which can represent the mouse button value, only refers to the keyboard key value. The comments in the source code are also misleading.

Add which for click:1 = = left; 2 = = middle; 3 = = Right


Note that here is the click, it is easy to let people use the click event, but actually click the event to get is wrong. Let's try this with the Click event:

  code is as follows copy code

<! DOCTYPE html>
<meta charset= "Utf-8"/>
<title></title>
<script src= "http://code.jquery.com/jquery-1.6.1.js" ></script>
<script type= "text/web Effects"
$ (document). Click (function (e) {
  alert (E.which);
})
</script>
<body>
</body>

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.