Home > Web Front-end > JS Tutorial > body text

Kibo Javascript tool library for handling keyboard events_javascript skills

WBOY
Release: 2016-05-16 18:00:15
Original
1139 people have browsed it

Getting Started
Kibo doesn’t depend on anything. You just put it in your HTML code:

Copy the code The code is as follows:



Downloadkibo.js
Create one Kibo object instance and event listener are established:
Copy code The code is as follows:

var k = new Kibo();

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

Syntax and usage
This tool library provides two simple methods - down and up. They both receive two parameters: a key or a combination of multiple keys or a wildcard, and a Function called when a matching key event occurs. Both methods can be called in a chain.

Key combination refers to a combination of control keys and character keys, or simply one or more control keys. You can pass a single key or multiple key combinations in an array to both methods.

When the function is called, the event will be passed into this function. If your event has no action, just ignore it. If the function returns false, the event's default action will be blocked.

Kibo provides a lastKey method to query the key that generated the last keyboard event. It will return the name of the key. If the last key pressed was an unsupported key, the method will return undefined. In addition, you can also use lastKey to determine whether a specific function key is pressed. It will either return true or false.

Supported keys
Kibo supports the following keys, and the key names are not case-sensitive when spelled:

Control keys shift, ctrl, alt

letters 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

Wildcards any, any arrow, any number, any letter, any f

Example
var k = new Kibo();

Single or multi-key combination
Copy code The code is 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 The code is as follows:

function handler() {
console.log(' last key: ' k.lastKey());
}

k.down(['shift q', 'ctrl alt x'], handler);

Wildcard
Copy code The 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');
} ;
k.down('f5', function() { return false; });


License
Kibo is released under the MIT License.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template