Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 30 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
#About
**jQuery.hotkeys** is a plug-in that lets you easily add and remove handlers for keyboard events anywhere in your code supporting almost any key combination.
**jQuery Hotkeys** is a plug-in that lets you easily add and remove handlers for keyboard events anywhere in your code supporting almost any key combination.

It is based on a library [Shortcut.js](http://www.openjs.com/scripts/events/keyboard_shortcuts/shortcut.js) written by [Binny V A](http://www.openjs.com/).
This plugin is based off of the plugin by Tzury Bar Yochay: [jQuery.hotkeys](http://github.com/tzuryby/hotkeys)

The syntax is as follows:
<pre>
$(expression).bind(<types>,<options>, <handler>);
$(expression).unbind(<types>,<options>, <handler>);

$(document).bind('keydown', 'Ctrl+a', fn);
$(expression).bind(types.keys, handler);
$(expression).unbind(types.keys, handler);

// e.g. replace '$' sign with 'EUR'
$('input.foo').bind('keyup', '$', function(){
this.value = this.value.replace('$', 'EUR');
});
$(document).bind('keydown.ctrl_a', fn);

$('div.foo').unbind('keydown', 'Ctrl+a', fn);
</pre>
## [Live Demo](http://jshotkeys.googlepages.com/test-static-01.html)
// e.g. replace '$' sign with 'EUR'
$('input.foo').bind('keyup.$', function(){
this.value = this.value.replace('$', 'EUR');
});

## Types
Supported types are `'keydown'`, `'keyup'` and `'keypress'`

## Options
The options are `'combi'` i.e. the key combination, and `'disableInInput'` which allow your code not to be executed when the cursor is located inside an input ( `$(elem).is('input') || $(elem).is('textarea')` ).
## Notes

As you can see, the key combination can be passed as string or as an object. You may pass an object in case you wish to override the default option for `disableInInput` which is set to `false`:
<pre>
$(document).bind('keydown', {combi:'a', disableinInput: true}, fn);
</pre>
I.e. when cursor is within an input field, `'a'` will be inserted into the input field without interfering.
If you want to use more than one modifiers (e.g. ctrl+alt+z) you should define them by an alphabetical order e.g. `alt_ctrl_z`

If you want to use more than one modifiers (e.g. alt+ctrl+z) you should define them by an alphabetical order e.g. alt+ctrl+shift
Hotkeys aren't tracked if you're inside of an input element (unless you explicitly bind the hotkey directly to the input). This helps to avoid conflict with normal user typing.

Modifiers are case insensitive, i.e. 'Ctrl+a' 'ctrl+a'.
All keys that have special meaning in RegExp strings (`\.+*?^$[](){}/'#`) can't
be removed by using the namespaced event until this jQuery bug get's fixed:
http://bugs.jquery.com/ticket/11458 the colon (`.`) will never work (even if bug
is fixed) since it is used to separate multiple namespaces. To remove them you
will need to remove events to the whole event type:

## Handler
In previous versions there was an option propagate which is removed now and implemented at the user code level.
// won't work
$('#foo').unbind('keyup.$');
$('#foo').unbind('keyup.+');

//will work
$('#foo').unbind('keyup');
$('#foo').unbind('keyup.a');
$('#foo').unbind('keyup.ctrl_s');

When using jQuery, if an event handler returns false, jQuery will call `stopPropagation()` and `preventDefault()`

## jQuery Compatibility
Tested with *jQuery 1.2.6*

Works with jQuery 1.4.2 and newer.

It known to be working with all the major browsers on all available platforms (Win/Mac/Linux)

Expand All @@ -51,43 +52,10 @@ It known to be working with all the major browsers on all available platforms (W
* Safari-3
* Chrome-0.2

## Features added in this version (0.7.x)
* Implemented as $.fn - let you use `this`.
* jQuery selectors are supported.
* Extending `$.fn.bind` and `$.fn.unbind` so you get a single interface for binding events to handlers

## Overriding jQuery
The plugin wraps the following jQuery methods:
* $.fn.bind
* $.fn.unbind
* $.find

Even though the plugin overrides these methods, the original methods will *always* be called.

The plugin will add functionality only for the `keydown`, `keyup` and `keypress` event types. Any other types are passed untouched to the original `'bind()'` and `'unbind()'` methods.

Moreover, if you call `bind()` without passing the shortcut key combination e.g. `$(document).bind('keydown', fn)` only the original `'bind()'` method will be executed.

I also modified the `$.fn.find` method by adding a single line at the top of the function body. here is the code:
### Addendum

<pre>
jQuery.fn.find = function( selector ) {
// the line I added
this.query=selector;
// call jQuery original find
return jQuery.fn.__find__.apply(this, arguments);
};
</pre>

You can read about this at [jQuery's User Group](http://groups.google.com/group/jquery-en/browse_thread/thread/18f9825e8d22f18d)

###Notes

Firefox is the most liberal one in the manner of letting you capture all short-cuts even those that are built-in in the browser such as `Ctrl-t` for new tab, or `Ctrl-a` for selecting all text. You can always bubble them up to the browser by returning `true` in your handler.
Firefox is the most liberal one in the manner of letting you capture all short-cuts even those that are built-in in the browser such as `Ctrl_t` for new tab, or `Ctrl_a` for selecting all text. You can always bubble them up to the browser by returning `true` in your handler.

Others, (IE) either let you handle built-in short-cuts, but will add their functionality after your code has executed. Or (Opera/Safari) will *not* pass those events to the DOM at all.

*So, if you bind `Ctrl-Q` or `Alt-F4` and your Safari/Opera window is closed don't be surprised.*


###Current Version is: beta 0.7
*So, if you bind `Ctrl_Q` or `Alt_F4` and your Safari/Opera window is closed don't be surprised.*
14 changes: 14 additions & 0 deletions component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "jquery.hotkeys",
"repo": "tzuryby/jquery.hotkeys",
"description": "jquery.hotkeys plugin lets you easily add and remove handlers for keyboard events",
"version": "1.0.0",
"keywords": [],
"dependencies": {},
"development": {},
"license": "MIT",
"main": "jquery.hotkeys.js",
"scripts": [
"jquery.hotkeys.js"
]
}
Loading