Home  >  Article  >  Web Front-end  >  jquery adds shortcut keys to the page example_jquery

jquery adds shortcut keys to the page example_jquery

WBOY
WBOYOriginal
2016-05-16 17:01:48930browse

Copy code The code is as follows:

$(document).keydown(function(e){
if(e.which == 37) {
alert("left");
}
if(e.which == 39) {
alert("right");
}
});

In addition, there is a keypress event that is similar to the keydown event, but cannot be mixed. The difference between keydown and keypress is:

1 Only character keys can trigger the keypress event, and any key can trigger the keydown event. For example: F1-F12, direction keys, etc. can only use keydown.

2 keydown returns the keyboard code, keypress returns the ASCII character, taking the character a as an example, keydown returns 65, and keypress returns 97.

If you want to detect Ctrl, Shift and other key combinations to determine whether these objects are true: e.shiftKey, e.ctrlKey

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn