Today, we will share a class library-keyboard that helps you add a shortcut key combination to your website or web application. js, you can easily capture the combination of input keys by using this class library, which can help you easily add related shortcut keys. Hope you like it!
Main features:
◆ Independent class libraries can also be used together with other class libraries, for example, jQuery
◆ Letter or letter combination binding
◆ Callback supported
◆ Multi-language support
◆ Supports AMD loading, such as RequireJS
◆ Complete documentation
Javascript:
- $(document).ready(function(){
- var gbin1 = ['g', 'b', 'i', 'n', '1'],
- google = ['g', 'o', 'o', 'g', 'l', 'e'],
- baidu = ['b', 'a', 'i', 'd', 'u'],
- kI = 0;
- document.addEventListener('keydown', function(){
- var keys = KeyboardJS.activeKeys();
- if(keys.length) {
- for(var i = 0; i < keys.length; i += 1) {
-
- if(keys[i] === gbin1[kI]) {
- if(kI < gbin1.length - 1) {
- kI += 1;
- } else {
- $("#info").html("Loading gbin1.com ... ...");
- location = "http://www.gbin1.com";
- }
- } else if(keys[i] === google[kI]) {
- if(kI < gbin1.length - 1) {
- kI += 1;
- } else {
- $("#info").html("Loading gbin1.com ... ...");
- location = "http://www.google.com";
- }
- } else if(keys[i] === baidu[kI]) {
- if(kI < gbin1.length - 1) {
- kI += 1;
- } else {
- $("#info").html("Loading gbin1.com ... ...");
- location = "http://www.baidu.com";
- }
- } else{
- kI = 0;
- }
-
- var keysString;
-
- keysString = keys.join(', ');
-
- if(keysString!=' '){
- var log=$("#log");
- log.append('<b style="display:none;border:1px solid #CCC;background:#000;color:#CCC;padding: 5px 10px;margin:5px">' + keysString + '</b>').find("b").last().show();
- }
-
- }
- }
- });
- });
HTML
- <div id="container">
-
-
- <ul>
- <li>gbin1</li>
- <li>google</li>
- <li>baidu</li>
- </ul>
- <div id="info">Status bar</div>
- </div>
CSS
- body{
- background: #ccc;
- }
-
- #container{
- margin: 0 auto;
- background: #202020;
- width: 960px;
- color: #E3E3E3;
- padding: 15px;
- margin-top: 0;
- }
-
- h3{
- font-size:16px;
- font-family: Arial;
- font-weight: normal;
- }
-
- #log b{
- position:relative;
- }
-
- #info{
- background: #303030;
- padding: 10px;
- font-size: 10px;
- color: #888;
- }
Original article: http://www.gbin1.com/technology/javascript/20120208keyboard4keyshortcutsupport/