This article introduces a js user who presses the keyboard to display the keyCode instance. For more information, see.
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"> <Html xmlns = "http://www.w3.org/1999/xhtml"> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/> <Title> press the keyboard to display the 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> </Head> <Body> <P> the keyCode you press is displayed here </p> </Body> </Html> |
Of course, you can also use jquery event. which to obtain the user's keyboard value.
Syntax
Event. which parameter description
Event is required. Specifies the event to be checked. 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, which is not supported by IE. Which is intended to get the keyCode of the keyboard ).
In jQuery, which can be the key value of the keyboard or the key value of the mouse.
That is, when you determine which key the user presses the keyboard, you can use which to determine which key the user presses the mouse. It is used in one fell swoop
The 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 is not 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 indicate the left, center, and right-click of the mouse. JQuery's which uses 1, 2, 3.
Another unpleasant thing is that the event. which in the jQuery document does not mention that which can represent the mouse key value, but only the keyboard key value. Comments in the source code are also misleading.
// Add which for click: 1 === left; 2 === middle; 3 === right
Note that click is used, which makes it easy to use the click event. However, it is incorrect to obtain the click event. Next, we will try the click event:
The Code is as follows: |
Copy code |
<! DOCTYPE html> <Html> <Head> <Meta charset = "UTF-8"/> <Title> </title> <Script src = "http://code.jquery.com/jquery-1.6.1.js"> </script> <Script type = "text/Webpage effects"> $ (Document). click (function (e ){ Alert (e. which ); }) </Script> </Head> <Body> </Body> </Html> |