Getting started
Kibo does not depend on anything. You just need to put it in your HTML code:
Copy codeThe Code is as follows: <script type = "text/javascript" src = "kibo. js"> </script>
Download kibo. js
Create a Kibo object instance and the event listener is created:Copy codeThe Code is as follows: var k = new Kibo ();
The Kibo object constructor has an optional parameter-the HTML element of the event you specified to receive. It can be input, textarea, or select. The actual usage is Doc ument.
Syntax and usage
The tool Library provides two simple methods: down and up. Both of them receive two parameters: a combination of buttons or multiple buttons or wildcards, and a function called when a matching button event occurs. Both methods can be called in a chain.
A key combination is a combination of a control key and a character key, or simply one or more control keys. You can input multiple key combinations in the form of one or one array to the two methods.
When a function is called, the event is passed in to the function. If your event has no operations, ignore it. If the function returns false, the default operation of the event is blocked.
Kibo provides a lastKey method to query the keys generated by the last keyboard event. It returns the key name. If the last key is not supported, the method returns undefined. In addition, you can use lastKey to determine whether a specific function key is pressed. It will either return true or return false.
Supported buttons
Kibo supports the following buttons, which are case-insensitive for spelling:
Control key shift, ctrl, alt
Keys from a to z
Number keys from 0 to 9
Function keys from f1 to f12
Direction keys left, up, right, down
Enter, esc, space, backspace, delete, insert, tab, page_up, page_down, home, end, caps_lock, num_lock
Wildcard: any, any arrow, any number, any letter, any f
Example
Var k = new Kibo ();
Single-or multi-key combinationCopy codeThe Code is as follows: k. down (['up', 'low'], function (){
Console. log ('up or down arrow key pressed ');
}). Up ('tab ', function (){
Console. log ('tab key released ');
});
Key combination with control keysCopy codeThe Code is as follows: function handler (){
Console. log ('Last key: '+ k. lastKey ());
}
K. down (['shift Q', 'ctrl alt x'], handler );
WildcardCopy codeThe Code is as follows: k. down (['any letter ', 'any number'], function (){
Console. log ('Letter or number key pressed ');
Console. log ('shift key was' + (k. lastKey ('shift ')? '': 'Not ') + 'pressed ');
});
K. up ('any', function (){
Console. log ('key released ');
});
Block default eventsCopy codeThe Code is as follows: k. down ('f5', function () {return false ;});
License
Kibo is released under the MIT License.