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
|
|
|
(function(jQuery){ |
14
|
|
|
"use strict"; |
15
|
|
|
jQuery.hotkeys = { |
16
|
|
|
version: "0.8", |
17
|
|
|
|
18
|
|
|
specialKeys: { |
19
|
|
|
8: "backspace", 9: "tab", 13: "return", 16: "shift", 17: "ctrl", 18: "alt", 19: "pause", |
20
|
|
|
20: "capslock", 27: "esc", 32: "space", 33: "pageup", 34: "pagedown", 35: "end", 36: "home", |
21
|
|
|
37: "left", 38: "up", 39: "right", 40: "down", 45: "insert", 46: "del", |
22
|
|
|
96: "0", 97: "1", 98: "2", 99: "3", 100: "4", 101: "5", 102: "6", 103: "7", |
23
|
|
|
104: "8", 105: "9", 106: "*", 107: "+", 109: "-", 110: ".", 111 : "/", |
24
|
|
|
112: "f1", 113: "f2", 114: "f3", 115: "f4", 116: "f5", 117: "f6", 118: "f7", 119: "f8", |
25
|
|
|
120: "f9", 121: "f10", 122: "f11", 123: "f12", 144: "numlock", 145: "scroll", 191: "/", 224: "meta" |
26
|
|
|
}, |
27
|
|
|
|
28
|
|
|
shiftNums: { |
29
|
|
|
"`": "~", "1": "!", "2": "@", "3": "#", "4": "$", "5": "%", "6": "^", "7": "&", |
30
|
|
|
"8": "*", "9": "(", "0": ")", "-": "_", "=": "+", ";": ": ", "'": "\"", ",": "<", |
31
|
|
|
".": ">", "/": "?", "\\": "|" |
32
|
|
|
} |
33
|
|
|
}; |
34
|
|
|
|
35
|
|
|
function keyHandler( handleObj ) { |
36
|
|
|
// Only care when a possible input has been specified |
37
|
|
|
if ( typeof handleObj.data !== "string" ) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
var origHandler = handleObj.handler, |
42
|
|
|
keys = handleObj.data.toLowerCase().split(" "), |
43
|
|
|
textAcceptingInputTypes = ["text", "password", "number", "email", "url", "range", "date", "month", "week", "time", "datetime", "datetime-local", "search", "color", "tel"]; |
44
|
|
|
|
45
|
|
|
handleObj.handler = function( event ) { |
46
|
|
|
// Don't fire in text-accepting inputs that we didn't directly bind to |
47
|
|
|
if ( this !== event.target && (/textarea|select/i.test( event.target.nodeName ) || |
48
|
|
|
jQuery.inArray(event.target.type, textAcceptingInputTypes) > -1 ) ) { |
49
|
|
|
return; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Keypress represents characters, not special keys |
53
|
|
|
var special = event.type !== "keypress" && jQuery.hotkeys.specialKeys[ event.which ], |
54
|
|
|
character = String.fromCharCode( event.which ).toLowerCase(), |
55
|
|
|
key, modif = "", possible = {}; |
|
|
|
|
56
|
|
|
|
57
|
|
|
// check combinations (alt|ctrl|shift+anything) |
58
|
|
|
if ( event.altKey && special !== "alt" ) { |
59
|
|
|
modif += "alt+"; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ( event.ctrlKey && special !== "ctrl" ) { |
63
|
|
|
modif += "ctrl+"; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// TODO: Need to make sure this works consistently across platforms |
67
|
|
|
if ( event.metaKey && !event.ctrlKey && special !== "meta" ) { |
68
|
|
|
modif += "meta+"; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ( event.shiftKey && special !== "shift" ) { |
72
|
|
|
modif += "shift+"; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ( special ) { |
76
|
|
|
possible[ modif + special ] = true; |
77
|
|
|
|
78
|
|
|
} else { |
79
|
|
|
possible[ modif + character ] = true; |
80
|
|
|
possible[ modif + jQuery.hotkeys.shiftNums[ character ] ] = true; |
81
|
|
|
|
82
|
|
|
// "$" can be triggered as "Shift+4" or "Shift+$" or just "$" |
83
|
|
|
if ( modif === "shift+" ) { |
84
|
|
|
possible[ jQuery.hotkeys.shiftNums[ character ] ] = true; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
for ( var i = 0, l = keys.length; i < l; i++ ) { |
89
|
|
|
if ( possible[ keys[i] ] ) { |
90
|
|
|
return origHandler.apply( this, arguments ); |
91
|
|
|
} |
92
|
|
|
} |
|
|
|
|
93
|
|
|
}; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
jQuery.each([ "keydown", "keyup", "keypress" ], function() { |
97
|
|
|
jQuery.event.special[ this ] = { add: keyHandler }; |
98
|
|
|
}); |
99
|
|
|
|
100
|
|
|
})( jQuery ); |