First, what you need to know is:
1, KeyDown ()
The KeyDown event is triggered when the keyboard is pressed.
2, KeyUp ()
The KeyUp event triggers when the key is released, which is what happens when you press the keyboard
3, KeyPress ()
The KeyPress event triggers when the keystroke is hit, which we can understand to press and lift the same key
Second, get the corresponding ASCII code on the keyboard:
Copy Code code as follows:
$ (document). KeyDown (function (event) {
Console.log (Event.keycode);
});
$tips: In the example above, Event.keycode can help us to get what we pressed on the keyboard, he returned the ASCII code, such as up and down key, respectively, is 38,40,37,39;
Iii. instance (when pressing the left and right side keys on the keyboard)
Copy Code code as follows:
$ (document). KeyDown (function (event) {
Judge when Event.keycode is 37 o'clock (that is, left side key), execute function to_left ();
Judge when Event.keycode is 39 o'clock (that is, right side key), execute function to_right ();
if (Event.keycode = = 37) {
do somethings;
}else if (Event.keycode = = 39) {
do somethings;
}
});
Case Study:
For example: The novel Web site in the common press about key to achieve the previous article and the next article; Press CTRL + ENTER to implement the form submission; Google Reader and Youdao reading in full shortcut key operation ... (To improve the user experience)
If we want to implement Ctrl+enter is CTRL + ENTER submit the form, you can do this:
Copy Code code as follows:
$ (document). KeyPress (function (e) {
if (e.ctrlkey && E.which = 13)
$ ("form"). Submit ();
})
Keyboard operation
$ (document). KeyDown (function (event) {
var e = Event | | window.event;
var k = E.keycode | | E.which;
Switch (k) {
Case 37:
...
Break
Case 39:
...
Break
}
return false;
})
A more detailed summary and presentation of events: http://www.jb51.net/article/28772.htm
jquery Event Reference Manual on the W3school
In the learning process, you should think more about how to improve interactivity and user experience.