This article briefly summarizes the usage of keyboard events in jQuery and related examples. it is very practical and can be referenced by anyone who needs it. I. First, you need to know:
1. keydown ()
The keydown event is triggered when the keyboard is pressed.
2. keyup ()
The keyup event is triggered when the key is released, that is, the event after you press the keyboard.
3. keypress ()
The keypress event is triggered when you press a key. we can understand it as pressing and lifting the same key.
2. obtain the ascII code corresponding to the keyboard:
$(document).keydown(function(event){ alert(event.keyCode); });
$ Tips: in the preceding example, event. the keyCode can help us obtain the keys on the keyboard that we press. the returned ascII code is, for example, the top, bottom, and left keys are, and 39;
III. instance (when the left and right sides of the keyboard are pressed)
The code is as follows:
$ (Document ). keydown (function (event) {// judge when event. when the keyCode is 37 (that is, the left side key), execute the to_left () function; // judge when the event. when the keyCode is 39 (that is, the right side key), execute the to_right () function; if (event. keyCode = 37) {to_left ();} else if (event. keyCode = 39) {to_right ();} else if (event. keyCode = 38) {to_top ();} else if (event. keyCode = 40) {to_bottom () ;}}); function to_left () {$ (". abc "cmd.css ({'left': '-= 10'});} function to_right () {$ (". abc ").css ({'left': '+ = 10'});} function to_top () {$ (". abc "cmd.css ({'top': '-= 10'});} function to_bottom () {$ (". abc "cmd.css ({'top': '+ = 10 '});}