Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event namespace prefix configuration #29

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ The syntax is as follows:
this.value = this.value.replace('$', 'EUR');
});

You can also configure some prefix for your events namespace, to avoid conflicts with other namespaces:

// Configure the prefix somewhere in your code
jQuery.hotkeys.prefix = "hk_";

// Then bind the hotkey
$(document).bind('keydown.hk_ctrl_a', fn);

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

Expand Down
10 changes: 6 additions & 4 deletions jquery.hotkeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
jQuery.hotkeys = {
version: "0.8+",

prefix: "",

specialKeys: {
8: "backspace", 9: "tab", 13: "return", 16: "shift", 17: "ctrl", 18: "alt", 19: "pause",
20: "capslock", 27: "esc", 32: "space", 33: "pageup", 34: "pagedown", 35: "end", 36: "home",
Expand Down Expand Up @@ -51,14 +53,14 @@
// Don't fire in text-accepting inputs that we didn't directly bind to
// important to note that $.fn.prop is only available on jquery 1.6+
if ( this !== event.target && (/textarea|select/i.test( event.target.nodeName ) ||
event.target.type === "text" || $(event.target).prop('contenteditable') == 'true' )) {
event.target.type === "text" || jQuery(event.target).prop('contenteditable') == 'true' )) {
return;
}

// Keypress represents characters, not special keys
var special = event.type !== "keypress" && jQuery.hotkeys.specialKeys[ event.which ],
character = String.fromCharCode( event.which ).toLowerCase(),
key, modif = "", possible = {};
key, prefix = jQuery.hotkeys.prefix, modif = prefix, possible = {};

// check combinations (alt|ctrl|shift+anything)
if ( event.altKey && special !== "alt" ) {
Expand Down Expand Up @@ -92,7 +94,7 @@
}

for ( var i = 0, l = keys.length; i < l; i++ ) {
if ( possible[ keys[i] ] ) {
if ( possible[ keys[i] ] || ( prefix.length > 0 && keys[i].indexOf( prefix ) != 0 ) ) {
return origHandler.apply( this, arguments );
}
}
Expand All @@ -103,4 +105,4 @@
jQuery.event.special[ this ] = { add: keyHandler };
});

})( jQuery );
})( jQuery );