|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>The key Event</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
<a href="#toc" class="jump">Jump to Table of Contents</a> |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
<h1>The key Event</h1> |
|
|
27 |
<div class="yui3-g"> |
|
|
28 |
<div class="yui3-u-3-4"> |
|
|
29 |
<div id="main"> |
|
|
30 |
<div class="content"><div class="intro"> |
|
|
31 |
<p>The <code>event-key</code> module adds the "key" event for subscribing to keyboard |
|
|
32 |
events triggered by users entering specific keys. The subscription |
|
|
33 |
signature includes a filter configuration that can be used to limit event |
|
|
34 |
triggering based on key codes, shift, ctrl, alt, or meta keys pressed, as |
|
|
35 |
well as specifying the keyboard event (<code>keydown</code>, <code>keyup</code>, or |
|
|
36 |
<code>keypress</code>).</p> |
|
|
37 |
</div> |
|
|
38 |
|
|
|
39 |
<h2 id="the-filtering-spec">The filtering spec</h2> |
|
|
40 |
|
|
|
41 |
<p>Example subscriptions might look like this:</p> |
|
|
42 |
|
|
|
43 |
<pre class="code prettyprint">// certain keys can be referenced by name |
|
|
44 |
input.on('key', saveAndClose, 'enter'); |
|
|
45 |
|
|
|
46 |
// require modifier keys with +(modifier) |
|
|
47 |
Y.one('doc').on('key', composeMail, 'n+ctrl'); |
|
|
48 |
|
|
|
49 |
// specify the event and key codes |
|
|
50 |
datatable.get('contentBox') |
|
|
51 |
.delegate('key', moveAround, 'down:37,38,39,40', '.yui3-datatable-liner');</pre> |
|
|
52 |
|
|
|
53 |
|
|
|
54 |
<p>The third argument is the filtering spec. Similar to using the |
|
|
55 |
<code>node.delegate()</code> method, the callback is only executed if the key event |
|
|
56 |
matches the filter. The supported filter syntax is a string defined like |
|
|
57 |
this:</p> |
|
|
58 |
|
|
|
59 |
<style> |
|
|
60 |
.railroad { |
|
|
61 |
margin-top: 2em; |
|
|
62 |
border-top: 2px solid #555; |
|
|
63 |
padding: 0 3em; |
|
|
64 |
text-align: center; |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
.railroad p { |
|
|
68 |
display: inline-block; |
|
|
69 |
*display: inline; zoom:1; |
|
|
70 |
veritical-align: top; |
|
|
71 |
position: relative; |
|
|
72 |
margin: 0 1.2em; |
|
|
73 |
padding: .2em; |
|
|
74 |
background: #fff; |
|
|
75 |
top: -.85em; |
|
|
76 |
font-size: 1.2em; |
|
|
77 |
font-weight: bold; |
|
|
78 |
*font-weight: normal; |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
.branch { |
|
|
82 |
position: relative; |
|
|
83 |
margin: 10px 20px 0; |
|
|
84 |
*margin-top: 0; |
|
|
85 |
border: 2px solid #555; |
|
|
86 |
border-top-width: 0; |
|
|
87 |
border-bottom-right-radius: 10px; |
|
|
88 |
border-bottom-left-radius: 10px; |
|
|
89 |
text-align: center; |
|
|
90 |
height: 1.25em; |
|
|
91 |
*height: 2em; |
|
|
92 |
line-height: 1.25em; |
|
|
93 |
*width: 7em; |
|
|
94 |
} |
|
|
95 |
.branch:before { |
|
|
96 |
display: block; |
|
|
97 |
position: absolute; |
|
|
98 |
left: -12px; |
|
|
99 |
top: -12px; |
|
|
100 |
content: ' '; |
|
|
101 |
height: 10px; |
|
|
102 |
width: 10px; |
|
|
103 |
border-top-right-radius: 10px; |
|
|
104 |
border: 2px solid #555; |
|
|
105 |
border-width: 2px 2px 0 0; |
|
|
106 |
} |
|
|
107 |
.branch:after { |
|
|
108 |
display: block; |
|
|
109 |
position: absolute; |
|
|
110 |
right: -12px; |
|
|
111 |
top: -12px; |
|
|
112 |
content: ' '; |
|
|
113 |
height: 10px; |
|
|
114 |
width: 10px; |
|
|
115 |
border-top-left-radius: 10px; |
|
|
116 |
border: 2px solid #555; |
|
|
117 |
border-width: 2px 0 0 2px; |
|
|
118 |
} |
|
|
119 |
.branch p { |
|
|
120 |
top: 0; |
|
|
121 |
*top: .8em; |
|
|
122 |
_top: 1em; |
|
|
123 |
line-height: 1.6em; |
|
|
124 |
z-index: 5; |
|
|
125 |
} |
|
|
126 |
|
|
|
127 |
.loop { |
|
|
128 |
position: relative; |
|
|
129 |
top: -2px; |
|
|
130 |
border: 2px solid #555; |
|
|
131 |
*border-top-width: 0; |
|
|
132 |
*top: 0; |
|
|
133 |
border-radius: 10px; |
|
|
134 |
text-align: center; |
|
|
135 |
height: 2.25em; |
|
|
136 |
margin: 0 20px; |
|
|
137 |
z-index: 4; |
|
|
138 |
} |
|
|
139 |
.loop p { |
|
|
140 |
top: .85em; |
|
|
141 |
z-index: 5; |
|
|
142 |
} |
|
|
143 |
|
|
|
144 |
.token { |
|
|
145 |
padding: .15em .4em .25em; |
|
|
146 |
color: #fff; |
|
|
147 |
text-shadow: -1px -1px 1px #471F1F; |
|
|
148 |
border-radius: .5em; |
|
|
149 |
|
|
|
150 |
background-color: #733; |
|
|
151 |
background-image: -webkit-linear-gradient(top, #a55, #733); |
|
|
152 |
background-image: -moz-linear-gradient(top, #a55, #733); |
|
|
153 |
background-image: -ms-linear-gradient(top, #a55, #733); |
|
|
154 |
background-image: -o-linear-gradient(top, #a55, #733); |
|
|
155 |
background-image: linear-gradient(top, #a55, #733); |
|
|
156 |
filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#aa5555', EndColorStr='#773333'); |
|
|
157 |
} |
|
|
158 |
|
|
|
159 |
#code-branch .branch, #code-branch .loop { |
|
|
160 |
width: 7em; |
|
|
161 |
} |
|
|
162 |
#mod-branch .branch, #mod-branch .loop { |
|
|
163 |
width: 10.5em; |
|
|
164 |
} |
|
|
165 |
</style> |
|
|
166 |
|
|
|
167 |
|
|
|
168 |
<ol> |
|
|
169 |
<li>(0..1) <span class="token">type</span> followed by a colon</li> |
|
|
170 |
<li>(0..n) comma separated <span class="token">code</span>s</li> |
|
|
171 |
<li>(0..n) <span class="token">modifier</span>s, each preceded by "+"</li> |
|
|
172 |
</ol> |
|
|
173 |
|
|
|
174 |
<h4 id="choo-choo">Choo choo!</h4> |
|
|
175 |
<p>If you're into railroad diagrams, the filter spec looks like this:</p> |
|
|
176 |
|
|
|
177 |
<ul class="railroad yui3-g"> |
|
|
178 |
<li class="yui3-u"> |
|
|
179 |
<p>"</p> |
|
|
180 |
</li> |
|
|
181 |
<li class="yui3-u"> |
|
|
182 |
<div class="branch"> |
|
|
183 |
<p><span class="token">type</span> :</p> |
|
|
184 |
</div> |
|
|
185 |
</li> |
|
|
186 |
<li class="yui3-u" id="code-branch"> |
|
|
187 |
<div class="branch"> |
|
|
188 |
<p><span class="token">code</span></p> |
|
|
189 |
</div> |
|
|
190 |
<div class="loop"> |
|
|
191 |
<p>,</p> |
|
|
192 |
</div> |
|
|
193 |
</li> |
|
|
194 |
<li class="yui3-u" id="mod-branch"> |
|
|
195 |
<div class="branch"> |
|
|
196 |
<p>+<span class="token">modifier</span></p> |
|
|
197 |
</div> |
|
|
198 |
<div class="loop"></div> |
|
|
199 |
</li> |
|
|
200 |
<li class="yui3-u"> |
|
|
201 |
<p>"</p> |
|
|
202 |
</li> |
|
|
203 |
</ul> |
|
|
204 |
|
|
|
205 |
<h4 id="filter-tokens">Filter tokens</h4> |
|
|
206 |
|
|
|
207 |
<dl> |
|
|
208 |
<dt><span class="token">type</span></dt> |
|
|
209 |
<dd>"down", "up", or "press" for <code>keydown</code>, <code>keyup</code>, and <code>keypress</code>. |
|
|
210 |
The default is <code>keypress</code>.</dd> |
|
|
211 |
|
|
|
212 |
<dt><span class="token">code</span><dt> |
|
|
213 |
<dd>Any numeric <code>keyCode</code>, unicode character, or <a |
|
|
214 |
href="#common">common key name</a>.</dd> |
|
|
215 |
|
|
|
216 |
<dt><span class="token">modifier</span></dt> |
|
|
217 |
<dd>"shift", "alt", "ctrl", or "meta". "meta" is the Windows key on |
|
|
218 |
Windows-friendly keyboards or the Command key on Apple keyboards. |
|
|
219 |
Remember each must be prefixed with "+".</dd> |
|
|
220 |
</dl> |
|
|
221 |
|
|
|
222 |
<h2 id="common">Common keys by name</h2> |
|
|
223 |
|
|
|
224 |
<p>Certain keys are common enough that referring to them by name is just easier |
|
|
225 |
and makes the code more readable. The supported key names are:</p> |
|
|
226 |
|
|
|
227 |
<table> |
|
|
228 |
<thead> |
|
|
229 |
<tr> |
|
|
230 |
<th>Name</th> |
|
|
231 |
<th><code>e.keyCode</code></th> |
|
|
232 |
</tr> |
|
|
233 |
</thead> |
|
|
234 |
<tbody> |
|
|
235 |
<tr> |
|
|
236 |
<td>enter</td> |
|
|
237 |
<td>13</td> |
|
|
238 |
</tr> |
|
|
239 |
<tr> |
|
|
240 |
<td>esc</td> |
|
|
241 |
<td>27</td> |
|
|
242 |
</tr> |
|
|
243 |
<tr> |
|
|
244 |
<td>backspace</td> |
|
|
245 |
<td>8</td> |
|
|
246 |
</tr> |
|
|
247 |
<tr> |
|
|
248 |
<td>tab</td> |
|
|
249 |
<td>9</td> |
|
|
250 |
</tr> |
|
|
251 |
<tr> |
|
|
252 |
<td>pageup</td> |
|
|
253 |
<td>33</td> |
|
|
254 |
</tr> |
|
|
255 |
<tr> |
|
|
256 |
<td>pagedown</td> |
|
|
257 |
<td>34</td> |
|
|
258 |
</tr> |
|
|
259 |
</tbody> |
|
|
260 |
</table> |
|
|
261 |
|
|
|
262 |
<p>If any of these are found in the spec, the default <span |
|
|
263 |
class="token">type</span> becomes <code>keydown</code>.</p> |
|
|
264 |
|
|
|
265 |
<p>If you have a mind to extend this map, it's stored in |
|
|
266 |
<code>Y.Node.DOM_EVENTS.key.eventDef.KEY_MAP</code>. For example, to add support for |
|
|
267 |
<code>node.on('key', callback, 'arrowup')</code>, you'd do:</p> |
|
|
268 |
|
|
|
269 |
<pre class="code prettyprint">Y.Node.DOM_EVENTS.key.eventDef.KEY_MAP.arrowup = 38;</pre> |
|
|
270 |
|
|
|
271 |
|
|
|
272 |
<h2 id="caveats">Caveats</h2> |
|
|
273 |
|
|
|
274 |
<ol> |
|
|
275 |
<li> |
|
|
276 |
You can't yet indicate that <span class="token">modifier</span>s must |
|
|
277 |
<em>not</em> be in effect or key combinations must <em>only</em> |
|
|
278 |
include those <span class="token">modifier</span>s. |
|
|
279 |
</li> |
|
|
280 |
<li> |
|
|
281 |
You can't yet specify <span class="token">type</span>s or <span |
|
|
282 |
class="token">modifier</span>s on a per-<span class="token">code</span> |
|
|
283 |
basis. |
|
|
284 |
</li> |
|
|
285 |
<li> |
|
|
286 |
Though you can specify keys by their common name, the event is not yet |
|
|
287 |
decorated in any way with that common name, so you still have to refer |
|
|
288 |
to the <code>keyCode</code> in callback code. |
|
|
289 |
</li> |
|
|
290 |
</ol> |
|
|
291 |
</div> |
|
|
292 |
</div> |
|
|
293 |
</div> |
|
|
294 |
|
|
|
295 |
<div class="yui3-u-1-4"> |
|
|
296 |
<div class="sidebar"> |
|
|
297 |
|
|
|
298 |
<div id="toc" class="sidebox"> |
|
|
299 |
<div class="hd"> |
|
|
300 |
<h2 class="no-toc">Table of Contents</h2> |
|
|
301 |
</div> |
|
|
302 |
|
|
|
303 |
<div class="bd"> |
|
|
304 |
<ul class="toc"> |
|
|
305 |
<li> |
|
|
306 |
<a href="#the-filtering-spec">The filtering spec</a> |
|
|
307 |
<ul class="toc"> |
|
|
308 |
<li> |
|
|
309 |
<a href="#choo-choo">Choo choo!</a> |
|
|
310 |
</li> |
|
|
311 |
<li> |
|
|
312 |
<a href="#filter-tokens">Filter tokens</a> |
|
|
313 |
</li> |
|
|
314 |
</ul> |
|
|
315 |
</li> |
|
|
316 |
<li> |
|
|
317 |
<a href="#common">Common keys by name</a> |
|
|
318 |
</li> |
|
|
319 |
<li> |
|
|
320 |
<a href="#caveats">Caveats</a> |
|
|
321 |
</li> |
|
|
322 |
</ul> |
|
|
323 |
</div> |
|
|
324 |
</div> |
|
|
325 |
|
|
|
326 |
|
|
|
327 |
|
|
|
328 |
<div class="sidebox"> |
|
|
329 |
<div class="hd"> |
|
|
330 |
<h2 class="no-toc">Examples</h2> |
|
|
331 |
</div> |
|
|
332 |
|
|
|
333 |
<div class="bd"> |
|
|
334 |
<ul class="examples"> |
|
|
335 |
|
|
|
336 |
|
|
|
337 |
<li data-description="Use the Event Utility to attach simple DOM event handlers."> |
|
|
338 |
<a href="basic-example.html">Simple DOM Events</a> |
|
|
339 |
</li> |
|
|
340 |
|
|
|
341 |
|
|
|
342 |
|
|
|
343 |
<li data-description="Using the synthetic event API to create a DOM event that fires in response to arrow keys being pressed."> |
|
|
344 |
<a href="synth-example.html">Creating an Arrow Event for DOM Subscription</a> |
|
|
345 |
</li> |
|
|
346 |
|
|
|
347 |
|
|
|
348 |
|
|
|
349 |
<li data-description="Supporting cross-device swipe gestures, using the event-move gesture events"> |
|
|
350 |
<a href="swipe-example.html">Supporting A Swipe Left Gesture</a> |
|
|
351 |
</li> |
|
|
352 |
|
|
|
353 |
|
|
|
354 |
|
|
|
355 |
|
|
|
356 |
|
|
|
357 |
|
|
|
358 |
|
|
|
359 |
|
|
|
360 |
|
|
|
361 |
|
|
|
362 |
|
|
|
363 |
|
|
|
364 |
</ul> |
|
|
365 |
</div> |
|
|
366 |
</div> |
|
|
367 |
|
|
|
368 |
|
|
|
369 |
|
|
|
370 |
<div class="sidebox"> |
|
|
371 |
<div class="hd"> |
|
|
372 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
373 |
</div> |
|
|
374 |
|
|
|
375 |
<div class="bd"> |
|
|
376 |
<ul class="examples"> |
|
|
377 |
|
|
|
378 |
|
|
|
379 |
|
|
|
380 |
|
|
|
381 |
|
|
|
382 |
|
|
|
383 |
|
|
|
384 |
|
|
|
385 |
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States."> |
|
|
386 |
<a href="../node-focusmanager/node-focusmanager-button.html">Accessible Menu Button</a> |
|
|
387 |
</li> |
|
|
388 |
|
|
|
389 |
|
|
|
390 |
|
|
|
391 |
<li data-description="Shows how to extend the base widget class, to create your own Widgets."> |
|
|
392 |
<a href="../widget/widget-extend.html">Extending the Base Widget Class</a> |
|
|
393 |
</li> |
|
|
394 |
|
|
|
395 |
|
|
|
396 |
|
|
|
397 |
<li data-description="Example Photo Browser application."> |
|
|
398 |
<a href="../dd/photo-browser.html">Photo Browser</a> |
|
|
399 |
</li> |
|
|
400 |
|
|
|
401 |
|
|
|
402 |
|
|
|
403 |
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation."> |
|
|
404 |
<a href="../dd/portal-drag.html">Portal Style Example</a> |
|
|
405 |
</li> |
|
|
406 |
|
|
|
407 |
|
|
|
408 |
|
|
|
409 |
<li data-description="Use IO to request data over HTTP."> |
|
|
410 |
<a href="../io/get.html">HTTP GET to request data</a> |
|
|
411 |
</li> |
|
|
412 |
|
|
|
413 |
|
|
|
414 |
</ul> |
|
|
415 |
</div> |
|
|
416 |
</div> |
|
|
417 |
|
|
|
418 |
</div> |
|
|
419 |
</div> |
|
|
420 |
</div> |
|
|
421 |
</div> |
|
|
422 |
|
|
|
423 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
424 |
<script>prettyPrint();</script> |
|
|
425 |
|
|
|
426 |
<script> |
|
|
427 |
YUI.Env.Tests = { |
|
|
428 |
examples: [], |
|
|
429 |
project: '../assets', |
|
|
430 |
assets: '../assets/event', |
|
|
431 |
name: 'event', |
|
|
432 |
title: 'The key Event', |
|
|
433 |
newWindow: '', |
|
|
434 |
auto: false |
|
|
435 |
}; |
|
|
436 |
YUI.Env.Tests.examples.push('basic-example'); |
|
|
437 |
YUI.Env.Tests.examples.push('synth-example'); |
|
|
438 |
YUI.Env.Tests.examples.push('swipe-example'); |
|
|
439 |
YUI.Env.Tests.examples.push('node-focusmanager-button'); |
|
|
440 |
YUI.Env.Tests.examples.push('widget-extend'); |
|
|
441 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
442 |
YUI.Env.Tests.examples.push('portal-drag'); |
|
|
443 |
YUI.Env.Tests.examples.push('get'); |
|
|
444 |
|
|
|
445 |
</script> |
|
|
446 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
447 |
|
|
|
448 |
|
|
|
449 |
|
|
|
450 |
</body> |
|
|
451 |
</html> |