Code for checking keys before jquery

Source: Internet
Author: User

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 small one. Program To test the buttons.

CopyCode 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.

implementation code: copy Code the code is as follows:

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: 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.