which
property is used to return the keyboard key or mouse button that is pressed when the current event is triggered .
For keyboard and mouse events, this property is used to determine which keyboard key or mouse button you are pressing.
which
The properties of the DOM are event.keyCode
event.charCode
normalized and are native.
The applicable event types are mainly keyboard events: KeyPress, KeyDown, KeyUp, and mouse events: MouseUp, MouseDown.
This property belongs to the Event
object (instance) of jquery.
JQuery 1.1.3 adds this property.
EventObject. which
return value
which
The return value of the property is the number type, which returns the keyboard key or mouse button that was pressed when the current event was triggered.
In the MouseDown,MouseUp event, the event.which
property returns the mapping code value (equivalent) of the corresponding mouse button event.button
. The following is a table of the main mouse button mapping codes.
Event.which Property Value |
the corresponding mouse button |
1 |
Left mouse button |
2 |
middle mouse button (wheel key) |
3 |
right mouse button |
In the KeyPress event, the event.which
property returns the Unicode value (equivalent) of the character entered event.charCode
. The following are common character Unicode code correspondence tables. For Unicode code for more characters, see full ASCII encoding table (Unicode compatible ASCII).
which property value (or range) |
the corresponding input character |
48-57 |
corresponding characters 0-9 |
65-90 |
Corresponding characters A-Z |
97-122 |
Corresponding characters A-Z
|
In the KeyDown,KeyUp event, the event.which
property returns the mapping code value (equivalent) of the corresponding key event.keyCode
. The following is a table of common keyboard key mapping codes:
which property value (or range) |
the corresponding keyboard keys |
8 |
Backspace key |
9 |
TAB key |
13 |
Enter key |
16 |
Shift key |
17 |
CTRL key |
20 |
ALT key |
20 |
Caps Lock key (case lock) |
27 |
ESC key |
33-36 |
corresponding keys PageUp, PageDown, End, Home |
37-40 |
Corresponding keys left, top, right, bottom (direction key) |
45-46 |
corresponding keys Insert, Delete |
48-57 |
corresponding keys 0-9 (non-keypad) |
65-90 |
Corresponding keys A-Z |
91 |
Windows key |
96-105 |
Corresponding key 0-9 (keypad) |
106, 107, 109, 110, 111 |
corresponding keys *, + 、-、.、/(keypad) |
112-123 |
Corresponding Key f1-f12
|
<id= "msg"></div>
The event.which
jquery sample code associated with the attribute is as follows (where the code for the form Validation section is only for simple, non-null validation):
//binding KeyDown and MouseDown two events for the current document//detects that the mouse has pressed those keys//detect the keyboard press the letter keys$ (document). Bind ("KeyDown MouseDown",function(event) {varmsg = "; if(Event.type = = "MouseDown") {//Mouse Down Events varMap = {"1": "Left", "2": "Medium", "3": "Right"}; Msg= ' You pressed the mouse [' + Map[event.which] + '] key '; }Else{//Keyboard Press Event if(Event.which >= && Event.which <= 90) {msg= ' You pressed the keyboard [' + String.fromCharCode (Event.which) + '] key '; } } if(msg) {$ ("#msg"). Prepend (msg + ' <br> ')); }});
$ (function () { $ ("body"). KeyDown (function (event) { if (Event.which = =) { $ ("#frm"). Submit (); } }) })
The event object (instance) of jquery.