Keyboard events in jquery _ jquery

Source: Internet
Author: User
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 '});}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.