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.