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 ');
}
})