Event Basics (ii) keyboard events

Source: Internet
Author: User



Keycod (key value, each key on the keyboard corresponds to a unique value)


Offsetwidth,offsetleft,offsetheight ... Represents the actual value of the

Getting the value of the keyboard requires an event object, and Firefox uses an EV

Keyboard controls the movement of Div

<! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">"http://www.w3.org/1999/xhtml">#div1 {width:100px; height:100px; background: #CCC; position:absolute;}</style><meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/><title> Untitled Document </title><script>Document.onkeydown=function (EV) {varoevent=ev| |Event; varOdiv=document.getelementbyid ('Div1'); //←37//Right        if(oevent.keycode==Panax Notoginseng) {ODiv.style.left=odiv.offsetleft-Ten+'px'; }    Else if(oevent.keycode== the) {ODiv.style.left=odiv.offsetleft+Ten+'px'; }};</script>"Div1"></div></body>Question: For example, in a tank war game, after pressing the arrow keys, the tank should continue to move, rather than move first, then pause, and then move on. How to solve this small problem? Keyboard Event Acquisition: Ctrl+enter message in fact, the function of keyboard events and mouse events is consistent, the function content is identical.
1<! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">2"http://www.w3.org/1999/xhtml">34<meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/>5<title> Untitled Document </title>6<script>7Window.onload=function ()8 {9     varObtn=document.getelementbyid ('BTN1');Ten     varOtxt1=document.getelementbyid ('txt1'); One     varOtxt2=document.getelementbyid ('txt2'); A      -obtn.onclick=function () -     { theOtxt1.value+=otxt2.value+'\ n'; -Otxt2.value="'; -     }; -      +otxt2.onkeydown=function (EV)// Remember: is an event added on the basis of a text box  -     { +         varoevent=ev| |Event; A          at         if(Oevent.ctrlkey && oevent.keycode== -) -         { -Otxt1.value+=otxt2.value+'\ n'; -Otxt2.value="'; -         } -     }; in }; -</script> to +  -<body> the<textarea id="txt1"Rows="Ten"cols=" +"></textarea><br/> *<input id="txt2"Type="text"/> $<input id="BTN1"Type="Button"Value="message"/>Panax Notoginseng</body> -

Event Basics (ii) keyboard events

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.