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: