JavaScript method for judging mouse buttons and keyboard keys

Source: Internet
Author: User

In the front-end design of the site, through the keyboard to control changes in page status, such as Up,down,pageup,pagedown and so on to the Web site scrolling. At present, the development of HTML5 brings a new game experience, so key operation is essential.

1. Monitor the mouse button.

Any keystroke operation must have a listener and a responder, so the mouse button is no exception. In the JS event operation, OnMouseDown is used to monitor the mouse button operation.

<!--Demo.html--> 

2. Monitor keyboard keys

<script type= "Text/javascript" >function KeyDown () {var value= event.keycode var key = String.fromCharCode (Eve Nt.keycode) Alert "key:" + key + "value:" + value "}//Document.onkeydown = KeyDown; Document.body[0].onkeydown = KeyDown; document.getElementById (' canvas '). OnKeyDown = KeyDown; </script>

Mainstream application: In HTML5, use keystrokes to control the movement of sprites, backgrounds and other elements in the game.

try doing it;


JavaScript method for judging mouse buttons and keyboard keys

Related Article

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.