Keyboard. js: JavaScript class library that can be added with a shortcut key combination

Source: Internet
Author: User

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:

 
 
  1. $(document).ready(function(){     
  2.     var gbin1 = ['g', 'b', 'i', 'n', '1'],  
  3.          google = ['g', 'o', 'o', 'g', 'l', 'e'],  
  4.          baidu = ['b', 'a', 'i', 'd', 'u'],  
  5.     kI = 0;  
  6.     document.addEventListener('keydown', function(){  
  7.         var keys = KeyboardJS.activeKeys();  
  8.         if(keys.length) {  
  9.             for(var i = 0; i < keys.length; i += 1) {  
  10.  
  11.                 if(keys[i] === gbin1[kI]) {  
  12.                     if(kI < gbin1.length - 1) {  
  13.                         kI += 1;  
  14.                     } else {  
  15.                         $("#info").html("Loading gbin1.com ... ...");  
  16.                         location = "http://www.gbin1.com";  
  17.                     }  
  18.                 } else if(keys[i] === google[kI]) {  
  19.                     if(kI < gbin1.length - 1) {  
  20.                         kI += 1;  
  21.                     } else {  
  22.                         $("#info").html("Loading gbin1.com ... ...");  
  23.                         location = "http://www.google.com";  
  24.                     }  
  25.                 } else if(keys[i] === baidu[kI]) {  
  26.                     if(kI < gbin1.length - 1) {  
  27.                         kI += 1;  
  28.                     } else {  
  29.                         $("#info").html("Loading gbin1.com ... ...");  
  30.                         location = "http://www.baidu.com";  
  31.                     }  
  32.                 } else{  
  33.                     kI = 0;  
  34.                 }  
  35.                   
  36.                 var keysString;  
  37.  
  38.                 keysString = keys.join(', ');  
  39.                   
  40.                 if(keysString!=' '){  
  41.                     var log=$("#log");  
  42.                     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();  
  43.                 }  
  44.  
  45.             }  
  46.         }     
  47.     });  
  48. }); 

HTML

 
 
  1. <div id="container"> 
  2.      
  3.     
  4.     <ul> 
  5.         <li>gbin1</li> 
  6.         <li>google</li> 
  7.         <li>baidu</li> 
  8.     </ul> 
  9.     <div id="info">Status bar</div> 
  10. </div> 

CSS

 
 
  1. body{  
  2.     background: #ccc;  
  3. }  
  4.  
  5. #container{  
  6.     margin: 0 auto;  
  7.     background: #202020;  
  8.     width: 960px;  
  9.     color: #E3E3E3;  
  10.     padding: 15px;  
  11.     margin-top: 0;  
  12. }  
  13.  
  14. h3{  
  15.     font-size:16px;  
  16.     font-family: Arial;  
  17.     font-weight: normal;  
  18. }  
  19.  
  20. #log b{  
  21.     position:relative;  
  22. }  
  23.  
  24. #info{  
  25.     background: #303030;  
  26.     padding: 10px;  
  27.     font-size: 10px;  
  28.     color: #888;  

Original article: http://www.gbin1.com/technology/javascript/20120208keyboard4keyshortcutsupport/

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.