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