2
YUI 3.13.0 (build 508226d)
3
Copyright 2013 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
8
YUI.add('event-key', function (Y, NAME) {
11
* Functionality to listen for one or more specific key combinations.
13
* @submodule event-key
33
_typeRE: /^(up|down|press):/,
34
_keysRE: /^(?:up|down|press):|\+(alt|ctrl|meta|shift)/g,
36
processArgs: function (args) {
37
var spec = args.splice(3,1)[0],
38
mods = Y.Array.hash(spec.match(/\+(?:alt|ctrl|meta|shift)\b/g) || []),
40
type: this._typeRE.test(spec) ? RegExp.$1 : null,
44
// strip type and modifiers from spec, leaving only keyCodes
45
bits = spec.replace(this._keysRE, ''),
49
bits = bits.split(',');
53
// FIXME: need to support '65,esc' => keypress, keydown
54
for (i = bits.length - 1; i >= 0; --i) {
57
// catch sloppy filters, trailing commas, etc 'a,,'
62
// non-numerics are single characters or key names
64
config.keys[chr] = mods;
66
lc = chr.toLowerCase();
68
if (this.KEY_MAP[lc]) {
69
config.keys[this.KEY_MAP[lc]] = mods;
70
// FIXME: '65,enter' defaults keydown for both
72
config.type = "down"; // safest
75
// FIXME: Character mapping only works for keypress
76
// events. Otherwise, it uses String.fromCharCode()
77
// from the keyCode, which is wrong.
79
uc = chr.toUpperCase();
85
// FIXME: stupid assumption that
86
// the keycode of the lower case == the
87
// charCode of the upper case
88
// a (key:65,char:97), A (key:65,char:65)
89
config.keys[chr.charCodeAt(0)] =
91
// upper case chars get +shift free
92
Y.merge(mods, { "+shift": true }) :
100
config.type = "press";
106
on: function (node, sub, notifier, filter) {
107
var spec = sub._extra,
108
type = "key" + spec.type,
110
method = (filter) ? "delegate" : "on";
112
// Note: without specifying any keyCodes, this becomes a
113
// horribly inefficient alias for 'keydown' (et al), but I
114
// can't abort this subscription for a simple
115
// Y.on('keypress', ...);
116
// Please use keyCodes or just subscribe directly to keydown,
117
// keyup, or keypress
118
sub._detach = node[method](type, function (e) {
119
var key = keys ? keys[e.which] : spec.mods;
122
(!key[ALT] || (key[ALT] && e.altKey)) &&
123
(!key[CTRL] || (key[CTRL] && e.ctrlKey)) &&
124
(!key[META] || (key[META] && e.metaKey)) &&
125
(!key[SHIFT] || (key[SHIFT] && e.shiftKey)))
132
detach: function (node, sub, notifier) {
133
sub._detach.detach();
137
eventDef.delegate = eventDef.on;
138
eventDef.detachDelegate = eventDef.detach;
141
* <p>Add a key listener. The listener will only be notified if the
142
* keystroke detected meets the supplied specification. The
143
* specification is a string that is defined as:</p>
147
* <dd><code>[{type}:]{code}[,{code}]*</code></dd>
149
* <dd><code>"down", "up", or "press"</code></dd>
151
* <dd><code>{keyCode|character|keyName}[+{modifier}]*</code></dd>
153
* <dd><code>"shift", "ctrl", "alt", or "meta"</code></dd>
155
* <dd><code>"enter", "backspace", "esc", "tab", "pageup", or "pagedown"</code></dd>
160
* <li><code>Y.on("key", callback, "press:12,65+shift+ctrl", "#my-input");</code></li>
161
* <li><code>Y.delegate("key", preventSubmit, "#forms", "enter", "input[type=text]");</code></li>
162
* <li><code>Y.one("doc").on("key", viNav, "j,k,l,;");</code></li>
167
* @param type {string} 'key'
168
* @param fn {function} the function to execute
169
* @param id {string|HTMLElement|collection} the element(s) to bind
170
* @param spec {string} the keyCode and modifier specification
171
* @param o optional context object
172
* @param args 0..n additional arguments to provide to the listener.
173
* @return {Event.Handle} the detach handle
175
Y.Event.define('key', eventDef, true);
178
}, '3.13.0', {"requires": ["event-synthetic"]});