Code for checking keys before jQuery _ jquery

Source: Internet
Author: User
I just saw a question: How do I know which keys are pressed at the same time and the key Order (is it a combination of fighting games ?), In fact, jQuery is easy to implement. During web development, you sometimes need to perform some operations based on the keyboard, such as submitting a form when you press Enter, prohibiting users from entering certain special characters, and setting shortcuts. At this time, you need to find out the keys that the user presses and write a applet to test the keys.

The Code is as follows:


$ (Document). ready (function (){
Var $ down = $ ("# down ");
Var $ press = $ ("# press ");
Var $ up = $ ("# up ");
$ (Document). keydown (function (event ){
$ Down. append (String. fromCharCode (event. keyCode) + "");
If (event. ctrlKey ){
Alert ("ctrl ");
}
}). Keyup (function (event ){
$ Up. append (String. fromCharCode (event. keyCode) + "");
}). Keypress (function (event ){
$ Press. append (String. fromCharCode (event. keyCode) + "");
});
});


When the trigger is down, the keyCode is pushed to the array and the repeated elements are deleted. When the trigger is up, $. grep is used to delete the keyCode from the array.
At any time, the array stores the previously pressed keys, which are arranged in the order of keys.
Use jQuery to determine the buttons currently pressed
The method is to use an external array to save the current button.
When keydown is triggered, the keyCode is pushed to the array and repeated elements are deleted. When keyup is triggered, $. grep is used to delete the keyCode from the array.

The implementation code is as follows:

The Code is as follows:


Current button:

Related Article

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.