Seventeen days ago (January 19, 2014), a new
version (2.0.0) of the Keypress javascript library was
released , designed for convenient programming of browser reactions to events from keystrokes (and from releases) of keys on the keyboard.
The list of changes published
on Github allows you to confidently announce the following: the most important advantage of the new version was the ability to catch events not only in the whole window, but also for one or several individual DOM elements. Now, when programming different reactions for different elements
on web pages, we can conveniently resort to the Keypress library instead of those earlier and more popular counterparts (for example,
jQuery.Hotkeys ), which are inferior to Keypress in wealth of possibilities.
![[Keypress]](https://habrastorage.org/getpro/habr/post_images/c73/6a8/82b/c736a882b60fcfbcc8c964c911544e1a.png)
')
The possibilities are:
- Reaction to pressing and releasing one or more keys. Supported synonyms are cross-platform (for example, "meta" means "cmd" or "ctrl" depending on the system) and encryption (for example, "@" means a combination of Shift and two).
Programmable keyboard shortcuts can include not only classic modifiers ("meta", "alt", "option", "ctrl", "shift", "cmd"), but also any other keys. For example, when programming WASD control with a step-by-step walker, it is not difficult to achieve that pressing and releasing the “W” key signifies a step forward, but “W” and “D” together - a step diagonally forward + to the right.
- It is possible to specify keyboard shortcuts, consisting not of simultaneous, but of successive keystrokes. These are combinations such as Konami code , for example.
- Special “counting” combinations allow you to program situations in which pressing the main key resets a certain counter, and each pressing of an additional key (holding the main key) increases this counter by one. David Mauro (by Keypress) cites the Tab + Space combination , in which pressing Tab immediately opens the first tab in a certain set of tabs, and each press of the space (while holding Tab) flips to the next tab - to the second, to the third, on the fourth ...
For each single key (or for a combination) additional settings are possible. These settings allow you to do this if necessary:
- Prevent reaction to the system auto-repeat keystrokes held down.
- Add to the handling also the interception of the event , that is, the method call event.preventDefault () .
- Control the specificity of events - for example, to prevent the "s" from triggering when pressing the "shift s" combination, if the reaction to this combination was programmed separately.
- Disable the requirement of strict key order. For example, to ensure the operation of "shift s" even in those cases when the "S" key was pressed first, and only after it - the "Shift" key.
- Prevent a key (or key combination) from being triggered when any other (third-party) keys (one or more) were pressed simultaneously with these keys.
Being aware of the benefits of such flexibility of definitions and settings, I intend to use the Keypress library in the future, and I wish you the same.