Original Javascript uses Mousetrap.js for keyboard event operations

Source: Internet
Author: User

In our daily development, we will encounter JS keyboard operations, such as enter the submission form. Or press a key to execute a method. Inadvertently found a size of less than 4K JS file, it is very convenient to operate keyboard events.

I also tried it: the specific code is as follows:

Details can go to its web browsing to see its API and download, address: Http://craig.is/killing/mice

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<script src= "Js/mousetrap.min.js" type= "Text/javascript" ></script>
<script src= "Js/jquery.min.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
$ (function () {
Mousetrap.bind (' 4 ', function () {alert ("4 key Pressed");});

Mousetrap.bind (' Enter ', function () {alert ("Press the ENTER Key");});
Mousetrap.bind (' x ', function () {alert ("pressed X");}, ' KeyUp ');

Mousetrap.bind (' Shift+k ', function (e) {
Alert ("Press the combination key Shift+k");
return false;
});

});</script>
<body>
</body>

I hereby record it. Easy to find later, but also hope to have just good people with this need to bring some help.

PS: Of course, there are other JS library options, such as Keyboard.js, Address: Https://github.com/RobertWHurst/KeyboardJS, but the personal feeling that the file is larger than 20 K. So there are not too many special needs, recommended Mousetrap.js

Original Javascript uses Mousetrap.js for keyboard event operations

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.