├── Gruntfile.js ├── package.json ├── hotkeys.jquery.json ├── test-static-07.html ├── test-static-03.html ├── test-static-04.html ├── test-static-06.html ├── README.md ├── test-static-02.html ├── jquery.hotkeys.js ├── test-static-01.html └── test-static-05.html /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | 3 | // Configuration. 4 | grunt.initConfig({ 5 | jshint: { 6 | options: { 7 | "undef": true, 8 | "unused": true 9 | }, 10 | 11 | files: ["jquery.hotkeys.js"] 12 | } 13 | }); 14 | 15 | // Task loading. 16 | grunt.loadNpmTasks("grunt-contrib-jshint"); 17 | 18 | // Task registration. 19 | grunt.registerTask("default", ["jshint"]); 20 | 21 | }; 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jquery.hotkeys", 3 | "version": "0.1.0", 4 | "description": "**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.", 5 | "main": "jquery.hotkeys.js", 6 | "devDependencies": { 7 | "grunt": "~0.4.1", 8 | "grunt-contrib-jshint": "~0.5.4" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/jeresig/jquery.hotkeys" 13 | }, 14 | "license": "MIT or GPL Version 2" 15 | } 16 | -------------------------------------------------------------------------------- /hotkeys.jquery.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hotkeys", 3 | "title": "jQuery Hotkeys", 4 | "description": "jQuery Hotkeys lets you watch for keyboard events anywhere in your code supporting almost any key combination.", 5 | "keywords": [ 6 | "keyboard", 7 | "events" 8 | ], 9 | "version": "0.1.0", 10 | "author": { 11 | "name": "John Resig", 12 | "url": "https://ejohn.org/" 13 | }, 14 | "maintainers": [ 15 | { 16 | "name": "John Resig", 17 | "email": "jeresig@gmail.com", 18 | "url": "http://ejohn.org/" 19 | } 20 | ], 21 | "licenses": [ 22 | { 23 | "type": "MIT", 24 | "url": "http://opensource.org/licenses/MIT" 25 | }, 26 | { 27 | "type": "GPLv2", 28 | "url": "http://www.gnu.org/licenses/gpl-2.0.html" 29 | } 30 | ], 31 | "homepage": "https://github.com/jeresig/jquery.hotkeys", 32 | "docs": "https://github.com/jeresig/jquery.hotkeys", 33 | "dependencies": { 34 | "jquery": ">=1.4.2" 35 | } 36 | } -------------------------------------------------------------------------------- /test-static-07.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 29 | 30 | -------------------------------------------------------------------------------- /test-static-03.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 |

Test #01

13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
32 | 33 | 34 | 35 | 42 | -------------------------------------------------------------------------------- /test-static-04.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | 11 | 29 | 30 | 31 | 32 |

jQuery.HotKeys.Testing.

33 |

Testing Platform:

34 |
35 |

Project Home: http://code.google.com/p/js-hotkeys/

36 |
37 |
38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /test-static-06.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |

KeyDown.

8 | currentTarget:
9 | which:
10 | shiftKey:
11 | ctrlKey:
12 | altKey:
13 |

KeyPress.

14 | currentTarget:
15 | which:
16 | shiftKey:
17 | ctrlKey:
18 | altKey:
19 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #About 2 | **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. 3 | 4 | This plugin is based off of the plugin by Tzury Bar Yochay: [jQuery.hotkeys](https://github.com/tzuryby/jquery.hotkeys) 5 | 6 | The syntax is as follows: 7 | 8 | $(expression).bind(types, keys, handler); 9 | $(expression).unbind(types, handler); 10 | 11 | $(document).bind('keydown', 'ctrl+a', fn); 12 | 13 | // e.g. replace '$' sign with 'EUR' 14 | $('input.foo').bind('keyup', '$', function(){ 15 | this.value = this.value.replace('$', 'EUR'); 16 | }); 17 | 18 | Syntax when wanting to use jQuery's `on()`/`off` methods: 19 | 20 | $(expression).on(types, null, keys, handler); 21 | $(expression).off(types, handler); 22 | 23 | $(document).on('keydown', null, 'ctrl+a', fn); 24 | 25 | // e.g. replace '$' sign with 'EUR' 26 | $('input.foo').on('keyup', null, '$', function(){ 27 | this.value = this.value.replace('$', 'EUR'); 28 | }); 29 | 30 | ## Types 31 | Supported types are `'keydown'`, `'keyup'` and `'keypress'` 32 | 33 | ## Notes 34 | 35 | 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 36 | 37 | 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. 38 | 39 | ## jQuery Compatibility 40 | 41 | Works with jQuery 1.4.2 and newer. 42 | 43 | It known to be working with all the major browsers on all available platforms (Win/Mac/Linux) 44 | 45 | * IE 6/7/8 46 | * FF 1.5/2/3 47 | * Opera-9 48 | * Safari-3 49 | * Chrome-0.2 50 | 51 | ### Addendum 52 | 53 | 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. 54 | 55 | 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. 56 | 57 | *So, if you bind `Ctrl-Q` or `Alt-F4` and your Safari/Opera window is closed don't be surprised.* 58 | -------------------------------------------------------------------------------- /test-static-02.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 |

Test #01

13 | 14 |

15 | type 'ctrl+l ' to focus.
16 | type 'shift+3' to insert 'Shift#' into the text box.
17 | type 'a' inside the textbox and have 'b' inserted instead. 18 |

19 |
20 |

Test #02

21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
40 | 41 | 42 | 43 |
44 |
45 | 46 | 47 | 88 | -------------------------------------------------------------------------------- /jquery.hotkeys.js: -------------------------------------------------------------------------------- 1 | /* 2 | * jQuery Hotkeys Plugin 3 | * Copyright 2010, John Resig 4 | * Dual licensed under the MIT or GPL Version 2 licenses. 5 | * 6 | * Based upon the plugin by Tzury Bar Yochay: 7 | * http://github.com/tzuryby/hotkeys 8 | * 9 | * Original idea by: 10 | * Binny V A, http://www.openjs.com/scripts/events/keyboard_shortcuts/ 11 | */ 12 | 13 | /* 14 | * One small change is: now keys are passed by object { keys: '...' } 15 | * Might be useful, when you want to pass some other data to your handler 16 | */ 17 | 18 | (function(jQuery){ 19 | 20 | jQuery.hotkeys = { 21 | version: "0.8", 22 | 23 | specialKeys: { 24 | 8: "backspace", 9: "tab", 10: "return", 13: "return", 16: "shift", 17: "ctrl", 18: "alt", 19: "pause", 25 | 20: "capslock", 27: "esc", 32: "space", 33: "pageup", 34: "pagedown", 35: "end", 36: "home", 26 | 37: "left", 38: "up", 39: "right", 40: "down", 45: "insert", 46: "del", 27 | 96: "0", 97: "1", 98: "2", 99: "3", 100: "4", 101: "5", 102: "6", 103: "7", 28 | 104: "8", 105: "9", 106: "*", 107: "+", 109: "-", 110: ".", 111 : "/", 29 | 112: "f1", 113: "f2", 114: "f3", 115: "f4", 116: "f5", 117: "f6", 118: "f7", 119: "f8", 30 | 120: "f9", 121: "f10", 122: "f11", 123: "f12", 144: "numlock", 145: "scroll", 186: ";", 191: "/", 31 | 220: "\\", 222: "'", 224: "meta" 32 | }, 33 | 34 | shiftNums: { 35 | "`": "~", "1": "!", "2": "@", "3": "#", "4": "$", "5": "%", "6": "^", "7": "&", 36 | "8": "*", "9": "(", "0": ")", "-": "_", "=": "+", ";": ": ", "'": "\"", ",": "<", 37 | ".": ">", "/": "?", "\\": "|" 38 | } 39 | }; 40 | 41 | function keyHandler( handleObj ) { 42 | if ( typeof handleObj.data === "string" ) { 43 | handleObj.data = { keys: handleObj.data }; 44 | } 45 | 46 | // Only care when a possible input has been specified 47 | if ( !handleObj.data || !handleObj.data.keys || typeof handleObj.data.keys !== "string" ) { 48 | return; 49 | } 50 | 51 | var origHandler = handleObj.handler, 52 | keys = handleObj.data.keys.toLowerCase().split(" "), 53 | textAcceptingInputTypes = ["text", "password", "number", "email", "url", "range", "date", "month", "week", "time", "datetime", "datetime-local", "search", "color", "tel"]; 54 | 55 | handleObj.handler = function( event ) { 56 | // Don't fire in text-accepting inputs that we didn't directly bind to 57 | if ( !handleObj.data.allowInTextInputField && this !== event.target && (/textarea|select/i.test( event.target.nodeName ) || 58 | jQuery.inArray(event.target.type, textAcceptingInputTypes) > -1 ) ) { 59 | return; 60 | } 61 | 62 | var special = jQuery.hotkeys.specialKeys[ event.keyCode ], 63 | // character codes are available only in keypress 64 | character = event.which > 0 && String.fromCharCode( event.which ).toLowerCase(), 65 | modif = "", possible = {}; 66 | 67 | // check combinations (alt|ctrl|shift+anything) 68 | if ( event.altKey && special !== "alt" ) { 69 | modif += "alt+"; 70 | } 71 | 72 | if ( event.ctrlKey && special !== "ctrl" ) { 73 | modif += "ctrl+"; 74 | } 75 | 76 | // TODO: Need to make sure this works consistently across platforms 77 | if ( event.metaKey && !event.ctrlKey && special !== "meta" ) { 78 | modif += "meta+"; 79 | } 80 | 81 | if ( event.shiftKey && special !== "shift" ) { 82 | modif += "shift+"; 83 | } 84 | 85 | if ( special ) { 86 | possible[ modif + special ] = true; 87 | } 88 | 89 | if ( character ) { 90 | possible[ modif + character ] = true; 91 | possible[ modif + jQuery.hotkeys.shiftNums[ character ] ] = true; 92 | 93 | // "$" can be triggered as "Shift+4" or "Shift+$" or just "$" 94 | if ( modif === "shift+" ) { 95 | possible[ jQuery.hotkeys.shiftNums[ character ] ] = true; 96 | } 97 | } 98 | 99 | for ( var i = 0, l = keys.length; i < l; i++ ) { 100 | if ( possible[ keys[i] ] ) { 101 | return origHandler.apply( this, arguments ); 102 | } 103 | } 104 | }; 105 | } 106 | 107 | jQuery.each([ "keydown", "keyup", "keypress" ], function() { 108 | jQuery.event.special[ this ] = { add: keyHandler }; 109 | }); 110 | 111 | })( this.jQuery ); 112 | -------------------------------------------------------------------------------- /test-static-01.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | 11 | 58 | 59 | 60 | 61 |

jQuery.HotKeys.Testing.

62 |

Testing Platform:

63 |
64 |

Project Home: http://code.google.com/p/js-hotkeys/

65 |
66 | 67 |

Special Keys

esc
tab
space
return
backspace
scroll
capslock
numlock
pause
insert
home
del
70 | 71 | end
pageup
pagedown
left
up
right
down
f1
f2
f3
f4
f5
f6
f7
f8
f9
f10
f11
f12
75 | 76 |

0-9 Digits

77 |
1
78 |
2
79 |
3
80 |
4
81 |
5
82 |
6
83 |
7
84 |
8
85 |
9
86 |
0
87 |
88 |

A-Z Letters

89 | 90 |
a
91 |
b
c
d
e
92 | 93 | f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v 96 | 97 |
w
x
y
z

Special Modifiers

Ctrl
Ctrl+a
Ctrl+b
Ctrl+c
Ctrl+d
Ctrl+e
Ctrl+f
Ctrl+g
Ctrl+h
Ctrl+i
Ctrl+j
Ctrl+k
101 | 102 | Ctrl+l
Ctrl+m
Ctrl+n
Ctrl+o
Ctrl+p
Ctrl+q
Ctrl+r
Ctrl+s
Ctrl+t
Ctrl+u
Ctrl+v
Ctrl+w
Ctrl+x
105 | Ctrl+y
Ctrl+z
Shift
Shift+a
Shift+b
Shift+c
Shift+d
Shift+e
Shift+f
Shift+g
Shift+h
Shift+i
Shift+j
Shift+k
Shift+l
Shift+m
Shift+n
Shift+o
Shift+p
Shift+q
Shift+r
Shift+s
Shift+t
Shift+u
Shift+v
Shift+w
Shift+x
Shift+y
Shift+z
112 | 113 | Alt
Alt+a
Alt+b
Alt+c
Alt+d
Alt+e
Alt+f
Alt+g
Alt+h
Alt+i
Alt+j
Alt+k
Alt+l
Alt+m
Alt+n
Alt+o
Alt+p
Alt+q
Alt+r
Alt+s
Alt+t
Alt+u
Alt+v
Alt+w
Alt+x
Alt+y
Alt+z

Special Modifiers + Special Keys

Ctrl
Ctrl+esc
Ctrl+tab
Ctrl+space
Ctrl+return
Ctrl+backspace
Ctrl+scroll 121 | 122 |
Ctrl+capslock
Ctrl+numlock
Ctrl+pause
Ctrl+insert
Ctrl+home
Ctrl+del
Ctrl+end
Ctrl+pageup
Ctrl+pagedown
Ctrl+left
Ctrl+up
Ctrl+right
Ctrl+down
Ctrl+f1
Ctrl+f2
Ctrl+f3
Ctrl+f4
Ctrl+f5
Ctrl+f6
Ctrl+f7
Ctrl+f8
Ctrl+f9
Ctrl+f10
Ctrl+f11
Ctrl+f12
Shift
Shift+esc
Shift+tab
Shift+space
Shift+return
Shift+backspace
Shift+scroll
Shift+capslock
Shift+numlock
Shift+pause
Shift+insert
Shift+home
Shift+del
Shift+end
Shift+pageup
Shift+pagedown
133 | 134 | Shift+left
Shift+up
Shift+right
Shift+down
Shift+f1
Shift+f2
Shift+f3
Shift+f4
Shift+f5
Shift+f6
Shift+f7
Shift+f8
Shift+f9
Shift+f10
Shift+f11
Shift+f12
Alt
Alt+esc
Alt+tab
Alt+space
Alt+return
Alt+backspace
Alt+scroll
Alt+capslock
Alt+numlock
Alt+pause
Alt+insert
Alt+home 141 | 142 |
Alt+del
Alt+end
Alt+pageup
Alt+ 143 | pagedown
Alt+left
Alt+up
Alt+right
Alt+ 144 | down
Alt+f1
Alt+f2
Alt+f3
Alt+f4
Alt+f5
Alt+f6
Alt+f7
Alt+f8
Alt+f9
Alt+f10
Alt+f11
Alt+f12
147 | 148 | 149 | 150 | -------------------------------------------------------------------------------- /test-static-05.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | 11 | 259 | 260 | 261 | 262 |

jQuery.HotKeys.Testing.

263 |

Testing Platform:

264 |
265 |

Project Home: http://code.google.com/p/js-hotkeys/

266 |
267 | 268 |

Special Keys

esc
tab
space
return
backspace
scroll
capslock
numlock
pause
insert
home
del
271 | 272 | end
pageup
pagedown
left
up
right
down
f1
f2
f3
f4
f5
f6
f7
f8
f9
f10
f11
f12
276 | 277 |

0-9 Digits

278 |
1
279 |
2
280 |
3
281 |
4
282 |
5
283 |
6
284 |
7
285 |
8
286 |
9
287 |
0
288 |
289 |

A-Z Letters

290 | 291 |
a
292 |
b
c
d
e
293 | 294 | f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v 297 | 298 |
w
x
y
z

Special Modifiers

Ctrl
Ctrl+a
Ctrl+b
Ctrl+c
Ctrl+d
Ctrl+e
Ctrl+f
Ctrl+g
Ctrl+h
Ctrl+i
Ctrl+j
Ctrl+k
302 | 303 | Ctrl+l
Ctrl+m
Ctrl+n
Ctrl+o
Ctrl+p
Ctrl+q
Ctrl+r
Ctrl+s
Ctrl+t
Ctrl+u
Ctrl+v
Ctrl+w
Ctrl+x
306 | Ctrl+y
Ctrl+z
Shift
Shift+a
Shift+b
Shift+c
Shift+d
Shift+e
Shift+f
Shift+g
Shift+h
Shift+i
Shift+j
Shift+k
Shift+l
Shift+m
Shift+n
Shift+o
Shift+p
Shift+q
Shift+r
Shift+s
Shift+t
Shift+u
Shift+v
Shift+w
Shift+x
Shift+y
Shift+z
313 | 314 | Alt
Alt+a
Alt+b
Alt+c
Alt+d
Alt+e
Alt+f
Alt+g
Alt+h
Alt+i
Alt+j
Alt+k
Alt+l
Alt+m
Alt+n
Alt+o
Alt+p
Alt+q
Alt+r
Alt+s
Alt+t
Alt+u
Alt+v
Alt+w
Alt+x
Alt+y
Alt+z

Special Modifiers + Special Keys

Ctrl
Ctrl+esc
Ctrl+tab
Ctrl+space
Ctrl+return
Ctrl+backspace
Ctrl+scroll 322 | 323 |
Ctrl+capslock
Ctrl+numlock
Ctrl+pause
Ctrl+insert
Ctrl+home
Ctrl+del
Ctrl+end
Ctrl+pageup
Ctrl+pagedown
Ctrl+left
Ctrl+up
Ctrl+right
Ctrl+down
Ctrl+f1
Ctrl+f2
Ctrl+f3
Ctrl+f4
Ctrl+f5
Ctrl+f6
Ctrl+f7
Ctrl+f8
Ctrl+f9
Ctrl+f10
Ctrl+f11
Ctrl+f12
Shift
Shift+esc
Shift+tab
Shift+space
Shift+return
Shift+backspace
Shift+scroll
Shift+capslock
Shift+numlock
Shift+pause
Shift+insert
Shift+home
Shift+del
Shift+end
Shift+pageup
Shift+pagedown
334 | 335 | Shift+left
Shift+up
Shift+right
Shift+down
Shift+f1
Shift+f2
Shift+f3
Shift+f4
Shift+f5
Shift+f6
Shift+f7
Shift+f8
Shift+f9
Shift+f10
Shift+f11
Shift+f12
Alt
Alt+esc
Alt+tab
Alt+space
Alt+return
Alt+backspace
Alt+scroll
Alt+capslock
Alt+numlock
Alt+pause
Alt+insert
Alt+home 342 | 343 |
Alt+del
Alt+end
Alt+pageup
Alt+ 344 | pagedown
Alt+left
Alt+up
Alt+right
Alt+ 345 | down
Alt+f1
Alt+f2
Alt+f3
Alt+f4
Alt+f5
Alt+f6
Alt+f7
Alt+f8
Alt+f9
Alt+f10
Alt+f11
Alt+f12
348 | 349 | 350 | 351 | --------------------------------------------------------------------------------