On the JSP page, you can use JavaScript to intercept key events on the keyboard
Source: Internet
Author: User
Keyboard control in the js| page page
Capturing keystrokes
The corresponding keystroke is the basic interaction between computer and human. You can control the press and release of any one of the keys. First, let's start with
Know how to start a so-called event. The following is a "onkeydown" event that initiates a key press.
Document.onkeydown = KeyDown
Here's the KeyDown is the corresponding keyboard subroutine you want to write. When your browser reads the above statement, it will know
Which key was pressed, and then start subroutine KeyDown (). The name of the subroutine goes with you, here in the subroutine name \ r
No parentheses are required after the word. Which key is being pressed is different in Netscape and IE. Look at the following code, if you are using the
NETSCAEP, the variable nkey will get the key code, and let the Iekey be 0. Conversely, if you are using IE, Iekey
For key code and Nkey to 0:
Code:
function KeyDown (e) {
if (NS4) {
var Nkey=e.which;
var iekey=0
}
if (IE4) {
var Iekey=event.keycode; var nkey=0
}
Alert ("Nkey:" +nkey+ "Iekey:" + iekey);
}
Document.onkeydown = KeyDown
if (NS4) document.captureevents (Event.keydown)
Moving elements with the keyboard
If you want to use the keyboard to start your slide, you need to know which key is being pressed and then use the appropriate subroutine to slide the element
We're going to use the "A" key to start the slide subroutine. Netscape's "A" is 97. and IE is 65. So
It is said that Nkey is 97 and Iekey is 65.
Code:
function init () {
if (NS4) block = Document.blockdiv
if (IE4) block = Blockdiv.style
Block.xpos = parseint (block.left)
Document.onkeydown = KeyDown
if (NS4) document.captureevents (Event.keydown)
}
function KeyDown (e) {
if (NS4) {
var Nkey=e.which; var iekey=0
}
if (IE4) {
var Iekey=event.keycode;
var nkey=0
}
if (nkey==97 | | iekey==65) {
If "A" key is pressed slide ()
}
}
function Slide () {
if (Block.xpos < 300) {
Block.xpos + 5 Block.left = block.xpos Status = Block.xpos
Not needed, just for show
settimeout ("Slide ()", 30)
}
}
Start variable
Here we introduce a method: Start the variable to control the stop and start of the slide. This variable records whether the element is moving
Still don't move. Then put an "if" statement in the slide subroutine, depending on the value of the startup variable to decide whether to stop or
Keep sliding.
Code:
function Slide () {
if (myobj.active) {
Myobj.xpos + 5
Myojb.left = Myobj.xpos
settimeout ("Slide ()", 30)
}
}
Use onKeyUp and startup variables to control sliding
OnKeyUp is the opposite of onkeydown, which means it responds to the event of a bond.
Code:
Document.onkeydown = KeyDown
Document.onkeyup = KeyUp if (NS4)
Document.captureevents (Event.keydown | Event.keyup)
The following is a complete program code:
function init () {if (NS4) block = Document.blockdiv if (ie4) block = Blockdiv.style Block.xpos = parseint (block.left) blo Ck.active = False Document.onkeydown = KeyDown Document.onkeyup = keyUp if (ns4) document.captureevents (Event.keydown | Event.keyup)} function KeyDown (e) {if (ns4) {var Nkey=e.which; var iekey=0} if (ie4) {var iekey=event.keycode; var NK Ey=0} if ((nkey==97 | | iekey==65) &&!block.active) {//if "A" key is pressed block.active = True Slide ()}} Fu Nction KeyUp (e) {if (ns4) {var Nkey=e.which; var iekey=0} if (ie4) {var iekey=event.keycode; var nkey=0} if (nkey==97 || iekey==65) {block.active = false//If "A" key is released}} function Slide () {if (block.active) {Block.xpos = 5 blo Ck.left = Block.xpos Status = Block.xpos//not needed, just to show settimeout ("Slide ()", 30)}}
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.