Kibo Javascript tool library for handling Keyboard Events

Source: Internet
Author: User

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.

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.