When writing the page, especially the desktop side, we sometimes have to know that the user pressed the button, for this problem we can use JS provided by the KeyCode properties to operate, such as:
Document.onkeydown = function (e) {
Alert (E.keycode)
}
At this time you are free to press the keyboard button on the page, will pop a pop-up window, which corresponds to the number of keys you press the KeyCode, this keycode is fixed, so we can use its value to judge, and then do some operations.
Document.onkeydown = function (e) {
if (E.keycode = = 13) {
Alert (' You press the OK key ');
}
if (E.keycode = = 84) {
Alert (' You pressed the T-key ');
}
}
To determine the keyboard up and down key events
Document.onkeydown=function (event) {
var e = Event | | window.event | | Arguments.callee.caller.arguments[0];
if (e && e.keycode==38 | | | e && e.keycode==37) {//up, left
Alert (' 38 = up key, 37 = left button ');
}
if (e && e.keycode==40 | | | e && e.keycode==39) {//down, right
Alert (' 38 = Next key, 37 = Right key ');
}
};