
Greetings to the habrasoobschestvo again. A week ago I
laid out my jQuery.keyboard . There were several flaws, unrealized ideas, and a huge number of suggestions and positive feedback prompted me to quickly write a new version.
Just want to thank
greedykid for support and debagging. If you like either, then he, too, can (and should) throw a plus.
Due to the fact that the
bug with incorrect keyCodes turned out to be my personal problem - I completely rewrote the engine, which gave me the opportunity to make my lib much more functional and extensible. The bugs found in the previous version were also fixed. This time, I am afraid, the phrase “In my opinion, everything is very simple and obvious” is no longer getting off, because I describe many of the features of the library (although I am quite sure that I can forget something).
Nevertheless, what I consider an undoubted advantage, it has kept the opportunity to remain as simple as in the previous version.
')
Examples
$. keyboard ( '( letters )+( numbers )' ,
function ( event ) {
// event[0].keyCode — letterKeyCode
// event[1].keyCode — numberKeyCode
}
);
Instruction
So, how to hang a keyboard event on an object. Except
$ ( obj ). keyboard ( /*args*/ )
$ ( obj ). keyboard ( /*args*/ )
from the previous version added the ability to hang the event like this:
$ . keyboard ( /*args*/ )
$ . keyboard ( /*args*/ )
, which is equivalent to
$ ( document ). keyboard ( /*args*/ )
$ ( document ). keyboard ( /*args*/ )
Arguments
$. keyboard ( object defaultConfig )
$. keyboard ( object defaultConfig )
- set default settings for the entire document
$. keyboard ( string keys ,[ object config ,] function fn )
$. keyboard ( string keys ,[ object config ,] function fn )
- hang a function to an object at a certain keystroke
$. keyboard ( string keys )
$. keyboard ( string keys )
- remove function on a certain keystroke
keys can still be an array, but this approach is already deprecated
Syntax keys:
The line is divided into an unlimited number of groups, separated by a comma:
« group1 , group2 , group3 »
. The event is in turn hung up on each of these groups of keys.
Each group has the following syntax: indices are separated by a space or a plus. If the indices are separated by a space, then the order is not important. If plus - the order is important. Combining space and plus in one group is impossible:
« key1 + key2 + key3 , key4 key5 key6 »
Each of the indices can be either the name of a particular key (
see the Index Table ), or the return code, Set or Options.
A set (or set of keys) is a set of keys similar in group. At the moment there are
8 sets by default . You can also set intervals by specifying the starting and last index. For example, (ak). The dialing is indicated in parentheses and means any of the dialing keys:
« key1 +( a - c )+( numbers ) »
.
The variant is similar to Set, compiled manually right in a line from Indexes and Set'ov. It is framed in square brackets and its parts are separated by the symbol “
| ":
« [ shift | alt | ( letters )] »
« [ shift | alt | ( letters )] »
« [ shift | alt | ( letters )] »
means shift, or alt, or any letter.
Plugin settings
Plugin settings - an object consisting (at the moment) of three properties (here are their default values):
settings = {
strict : true ,
event : 'keydown' ,
preventDefault : false
}
(Strict == true) - means that this combination will not work if any keys are pressed, except for those specified in the combination
(Event) - means the event under which the action will be performed. At the moment, “keydown” and “keyup” are available, but if necessary, I can add more.
(preventDefault) - means calling the appropriate event method. It should be taken into account that, afaik, all default-events occur at the keydown event, therefore it is most expected to work in this case
Functions
Two arguments are passed to the function:
func ( events , bind )
. this refers to the current jQuery object
events contains an array of event keystrokes (only those that are in the request), that is, for example, when calling
$.( '( letters )+shift' , function ( e , bind ) {})
to
e [0]. keyCode
e [0]. keyCode
can find out exactly which letter was pressed. Bind contains trail information (in fact, all information is hardly needed except
bind.keys.group
is the sequence number of the triggered group. For example, in
$ . keyboard ( 'alt, shift' )
when you press
« alt »
bind.keys.group
will be equal to 0):
bind = {
cfg : {}, // ,
func : function // — ( )
keys : {} //
}
Known Issues
1. It is possible that in some places preventDefault does not work correctly (depends on the browser?)
2. Unresolved
problem with null keyCode'ami , which, perhaps, manifests itself when the keyboard driver is incorrectly configured, which, judging by the statistics, is extremely rare (so far only detected by the developer jQuery.keyboard). Who knows anything about her - invite to the
topicmiscellanea
1. I have not yet decided on terminology, therefore offer my options.
2. Minor changes to the interface are possible, but I will make every effort so that the new versions are backward compatible with the previous ones (at least with the help of the appropriate settings)
3. This is version 0.2.0, that is, the second Beta
4. Wishes and suggestions are accepted.
Links
Demo