jquery to determine whether the keyboard or the mouse method

Source: Internet
Author: User

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-use.
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 isn't normalized, so don ' t use it
if (!event.which && event.button!== undefined) {
Event.which = (Event.button & 1 1: (Event.button & 2 3: (Event.button & 4 2:0));
}

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.
Copy code code as follows:
Add which for click:1 = = left; 2 = = middle; 3 = = Right

Note that here is the click, it's easy to get people to use the Click event, but actually getting it in the Click event is wrong.
Try the Click event below:
Copy the Code as follows:
<! DOCTYPE html>
<meta charset= "Utf-8"/>
<title></title>
<script src= "Http://code.jquery.com/jquery-1.6.1.js" > <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.