Instance
Show which key was pressed:
$ ("input"). KeyDown (function (event) {
$ ("div"). html ("Key:" + Event.which);
});
Give it a shot yourself.
Definitions and usage
The Which property indicates which key or button was pressed.
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-use.
Source:
Copy Code code as follows:
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));
}
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:
Copy Code code as follows:
<! DOCTYPE html>
<meta charset= "Utf-8"/>
<title></title>
<script src= "Http://demo.jb51.net/jslib/jquery/jquery-1.6.1.js" ></script>
<script type= "Text/css" >
$ (document). Click (function (e) {
alert (E.which);
})
</script>
<body>
</body>