Kibo JavaScript Tool library for handling keyboard events _javascript Tips

Source: Internet
Author: User
Tags event listener
Entry
Kibo is not dependent on anything. All you have to do is put it in your HTML code:
Copy Code code as follows:

<script type= "Text/javascript" src= "Kibo.js" ></script>

Download Kibo.js
Creating a Kibo object instance, the event listener is established:
Copy Code code as follows:

var k = new Kibo ();

The constructor for the Kibo object has an optional parameter-the HTML element that you specify to receive the event. Can be input, textarea or select, and by default is Window.document.

Syntax and usage
This tool library provides two simple methods--down and up, all of which receive two parameters: a combination or wildcard character of a key or multiple keystrokes, and a function that is called when a matching keystroke event occurs. Both of these methods can be called by chain.

A key combination is a combination of a control key and a word keys, or simply one or more control keys. You can pass these two methods into multiple key combinations in a single or an array form.

When a function is called, the event is passed into this function. If you have nothing to do with your event, just ignore it. If the function returns FALSE, the default action for the event is blocked.

The Kibo provides a Lastkey method for querying what keys the last keyboard event was generated from. It returns the name of the key, and if the key is not supported at the last keystroke, the method returns undefined. In addition, you can use Lastkey to determine whether a particular function key is pressed, it will either return TRUE or return false.

Supported Keys
Kibo supports the following keys, which are not case-sensitive when spelling these key names:

Control key SHIFT, CTRL, ALT

Letter keys from A to Z

Number keys from 0 to 9

function keys from F1 to F12

Direction key left, up, right, down

Enter, ESC, spaces, backspace, delete, insert, tab, PAGE_UP, Page_down, home, End, Caps_lock, Num_lock

Wildcard any, any arrow, any number, or letter or any F

Example
var k = new Kibo ();

Single or multiple key combinations
Copy Code code as follows:

K.down ([' Up ', ' down '], function () {
Console.log (' Up or DOWN ARROW key pressed ');
}. Up (' tab ', function () {
Console.log (' TAB key released ');
});

Key combinations containing control keys
Copy Code code as follows:

function handler () {
Console.log (' last key: ' + K.lastkey ());
}

K.down ([' Shift Q ', ' Ctrl alt X '], handler);

Wildcard characters
Copy Code code 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 Events
Copy Code code 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.