Example of getting keyboard and mouse keys by event. which under jquery

Source: Internet
Author: User

Use Event.which in jquery to get keyboard, mouse key values. Event.which Event.keycode and Event.charcode standardized.
Get keyboard input values

<! DOCTYPE html>
<meta charset= "Utf-8"/>
<script type= "Text/javascript" src= "Http://lib.sinaapp.com/js/jquery/1.8/jquery.min.js" ></script>
<script type= "Text/javascript" >
$ (function () {
$ ("#txt"). KeyDown (function (event) {
$ ("#msg"). HTML (Event.which);
});
})
</script>
<body>
<div id= "MSG" ></div>
<input id= "txt" type= "text" value= ""/>
</body>

In the example above, the event is a jquery event object and is required. Specify the events to check. This event parameter comes from the event-binding function.

The jquery event property which can get the key value of the keyboard, and the which property is still reliable across browsers. It is recommended that you use Event.which to monitor keyboard input instead of the KeyCode attribute.

A reliable, case-sensitive character encoding can be obtained through the which property of the KeyPress event. However, non-alphabetic symbols are not reliable across browsers.
With the which property of the KeyUp and KeyDown events, you can only get case-insensitive key encodings (so both a and a return 65), but you can determine the case by checking the Shiftkey property. For non-alphabetic symbols (for example: the left arrow encoding is 37) can be reliably run.

Gets the key value of the mouse

<! DOCTYPE html>
<meta charset= "Utf-8"/>
<script type= "Text/javascript" src= "Http://lib.sinaapp.com/js/jquery/1.8/jquery.min.js" ></script>
<script type= "Text/javascript" >
$ (function () {
$ (document). MouseDown (function (event) {
alert (Event.which);
});
})
</script>
<body>
</body>

For mouse events, the event property which can also get the mouse key value (1 is left, 2 is in, 3 is right), and the which property spans multiple browsers in a consistent manner. You should therefore take advantage of the which instead of the button attribute.

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.