I wanted to use hotkeys to control content in my project. I pressed esc - the dialog box was closed, ctrl + left - the next section opened ... I rummaged through Google-Yandex and, not finding the ideal, decided to write my solution.
Now I have it - small, light, independent from other libraries.
It is enough for me to connect a javascript file and write
$e( 'ctrl esc' ).bind( "alert('');" );
Ready javascript can be found
here .
UPD Improved code. Thank you,
Yeah ,
aubt .
Read more about the possibilities
$ E () function
$ e (param0 [[, param1 [, param2 ...]])The function accepts a list of hot keys (case-insensitive, separated by spaces or "+") for which you want to hang an action.
Understands: alt, shift, ctrl, meta, esc, enter, up, down, left, right, tab, space, pageup, pagedown, home, end, Latin letters (a-zA-Z), numbers (0-9 ), character codes (10-128);
In addition, there are special words keyup, keydown, keypress, which indicate which action to respond to (by default - keyup).
It must be remembered that browsers may or may not trigger events depending on its type.Returns the context.
Examples:
$e( 'esc' )
$e( 'keyup Ctrl+left' , 'meta left keyup' )
.Bind () function
.bind (param)It takes a function or javascript, which will be assigned to the key combination passed by the $ e () function.
')
Examples:
$e( 'esc' ).bind( 'alert("!")' )
$e( 'keyup Ctrl+left' , 'meta left keyup' ).bind(do_foo)
Function .unbind ()
.unbind ([param])Unlink an action from the shortcut key passed by the $ e () function.
If the parameter is empty, then all actions will be unbound from this combination.
Example:
$e( 'esc' , 'keyup Ctrl+left' , 'meta left keyup' ).unbind()
The $ e.start () function
Starts listening to keys.
Listening also turns on by itself when using the .bind () function.
The $ e.stop () function
Stops listening to keys.
* This source code was highlighted with Source Code Highlighter .