The keyboard event for jquery events

Source: Internet
Author: User

first, need to know is:
1, KeyDown ()
The KeyDown event is triggered when the keyboard is pressed.
2, KeyUp ()
The KeyUp event is triggered when the key is released, which is the event you press the keyboard up
3, KeyPress ()
The KeyPress event is triggered when the button is tapped, and we can understand that pressing and lifting the same button


Second, obtain the corresponding ASCII code on the keyboard:
$ (document). KeyDown (function (event) {
alert (Event.keycode);
});

$tips: In the example above, Event.keycode can help us to get to what button we pressed on the keyboard, he returned the ASCII code, such as the next key around, is 38,40,37,39;

It started with KeyPress, but this does not work under Chrome, and then you can do it with KeyUp, which gets the keyboard keys in jquery, you can use the which property to get the key values, but not all key events can be captured to their key values ( Keyup,keypress,keydown), tested, only the KeyUp event can capture key values in almost all browsers, KeyDown does not work in IE6.

Test browser: Ie6/ie7/ie8,firefox,chrome,opera

$ (document). KeyUp (function (e) {
var key = E.which;
if (key = = 27) {

Alert (' Press the ESC key to close the popup layer ');
}
});

Or (This only in: Ie8,firefox,chrome tested)

$ (document). KeyUp (function (e) {

if (E.keycode = = 27)
{
Alert (' Press the ESC key to close the popup layer ');
}

})


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.