5
<title>The key Event</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>The key Event</h1>
17
<a href="#toc" class="jump">Jump to Table of Contents</a>
21
<div id="main" class="yui3-u">
22
<div class="content"><div class="intro">
23
<p>The <code>event-key</code> module adds the "key" event for subscribing to keyboard
24
events triggered by users entering specific keys. The subscription
25
signature includes a filter configuration that can be used to limit event
26
triggering based on key codes, shift, ctrl, alt, or meta keys pressed, as
27
well as specifying the keyboard event (<code>keydown</code>, <code>keyup</code>, or
28
<code>keypress</code>).</p>
31
<h2 id="the-filtering-spec">The filtering spec</h2>
33
<p>Example subscriptions might look like this:</p>
35
<pre class="code prettyprint">// certain keys can be referenced by name
36
input.on('key', saveAndClose, 'enter');
38
// require modifier keys with +(modifier)
39
Y.one('doc').on('key', composeMail, 'n+ctrl');
41
// specify the event and key codes
42
datatable.get('contentBox')
43
.delegate('key', moveAround, 'down:37,38,39,40', '.yui3-datatable-liner');</pre>
46
<p>The third argument is the filtering spec. Similar to using the
47
<code>node.delegate()</code> method, the callback is only executed if the key event
48
matches the filter. The supported filter syntax is a string defined like
54
border-top: 2px solid #555;
60
display: inline-block;
61
*display: inline; zoom:1;
77
border: 2px solid #555;
79
border-bottom-right-radius: 10px;
80
border-bottom-left-radius: 10px;
95
border-top-right-radius: 10px;
96
border: 2px solid #555;
97
border-width: 2px 2px 0 0;
107
border-top-left-radius: 10px;
108
border: 2px solid #555;
109
border-width: 2px 0 0 2px;
122
border: 2px solid #555;
123
*border-top-width: 0;
137
padding: .15em .4em .25em;
139
text-shadow: -1px -1px 1px #471F1F;
142
background-color: #733;
143
background-image: -webkit-linear-gradient(top, #a55, #733);
144
background-image: -moz-linear-gradient(top, #a55, #733);
145
background-image: -ms-linear-gradient(top, #a55, #733);
146
background-image: -o-linear-gradient(top, #a55, #733);
147
background-image: linear-gradient(top, #a55, #733);
148
filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#aa5555', EndColorStr='#773333');
151
#code-branch .branch, #code-branch .loop {
154
#mod-branch .branch, #mod-branch .loop {
161
<li>(0..1) <span class="token">type</span> followed by a colon</li>
162
<li>(0..n) comma separated <span class="token">code</span>s</li>
163
<li>(0..n) <span class="token">modifier</span>s, each preceded by "+"</li>
166
<h4 id="choo-choo">Choo choo!</h4>
167
<p>If you're into railroad diagrams, the filter spec looks like this:</p>
169
<ul class="railroad yui3-g">
175
<p><span class="token">type</span> :</p>
178
<li class="yui3-u" id="code-branch">
180
<p><span class="token">code</span></p>
186
<li class="yui3-u" id="mod-branch">
188
<p>+<span class="token">modifier</span></p>
190
<div class="loop"></div>
197
<h4 id="filter-tokens">Filter tokens</h4>
200
<dt><span class="token">type</span></dt>
201
<dd>"down", "up", or "press" for <code>keydown</code>, <code>keyup</code>, and <code>keypress</code>.
202
The default is <code>keypress</code>.</dd>
204
<dt><span class="token">code</span><dt>
205
<dd>Any numeric <code>keyCode</code>, unicode character, or <a
206
href="#common">common key name</a>.</dd>
208
<dt><span class="token">modifier</span></dt>
209
<dd>"shift", "alt", "ctrl", or "meta". "meta" is the Windows key on
210
Windows-friendly keyboards or the Command key on Apple keyboards.
211
Remember each must be prefixed with "+".</dd>
214
<h2 id="common">Common keys by name</h2>
216
<p>Certain keys are common enough that referring to them by name is just easier
217
and makes the code more readable. The supported key names are:</p>
223
<th><code>e.keyCode</code></th>
254
<p>If any of these are found in the spec, the default <span
255
class="token">type</span> becomes <code>keydown</code>.</p>
257
<p>If you have a mind to extend this map, it's stored in
258
<code>Y.Node.DOM_EVENTS.key.eventDef.KEY_MAP</code>. For example, to add support for
259
<code>node.on('key', callback, 'arrowup')</code>, you'd do:</p>
261
<pre class="code prettyprint">Y.Node.DOM_EVENTS.key.eventDef.KEY_MAP.arrowup = 38;</pre>
264
<h2 id="caveats">Caveats</h2>
268
You can't yet indicate that <span class="token">modifier</span>s must
269
<em>not</em> be in effect or key combinations must <em>only</em>
270
include those <span class="token">modifier</span>s.
273
You can't yet specify <span class="token">type</span>s or <span
274
class="token">modifier</span>s on a per-<span class="token">code</span>
278
Though you can specify keys by their common name, the event is not yet
279
decorated in any way with that common name, so you still have to refer
280
to the <code>keyCode</code> in callback code.
286
<div id="sidebar" class="yui3-u">
288
<div id="toc" class="sidebox">
290
<h2 class="no-toc">Table of Contents</h2>
296
<a href="#the-filtering-spec">The filtering spec</a>
299
<a href="#choo-choo">Choo choo!</a>
302
<a href="#filter-tokens">Filter tokens</a>
307
<a href="#common">Common keys by name</a>
310
<a href="#caveats">Caveats</a>
318
<div class="sidebox">
320
<h2 class="no-toc">Examples</h2>
324
<ul class="examples">
327
<li data-description="Use the Event Utility to attach simple DOM event handlers.">
328
<a href="basic-example.html">Simple DOM Events</a>
333
<li data-description="Using the synthetic event API to create a DOM event that fires in response to arrow keys being pressed.">
334
<a href="synth-example.html">Creating an Arrow Event for DOM Subscription</a>
339
<li data-description="Supporting cross-device swipe gestures, using the event-move gesture events">
340
<a href="swipe-example.html">Supporting A Swipe Left Gesture</a>
360
<div class="sidebox">
362
<h2 class="no-toc">Examples That Use This Component</h2>
366
<ul class="examples">
375
<li data-description="Shows how to extend the base widget class, to create your own Widgets.">
376
<a href="../widget/widget-extend.html">Extending the Base Widget Class</a>
381
<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.">
382
<a href="../node-focusmanager/node-focusmanager-3.html">Accessible Menu Button</a>
387
<li data-description="Use IO to request data over HTTP.">
388
<a href="../io/get.html">HTTP GET to request data</a>
393
<li data-description="Example Photo Browser application.">
394
<a href="../dd/photo-browser.html">Photo Browser</a>
399
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation.">
400
<a href="../dd/portal-drag.html">Portal Style Example</a>
412
<script src="../assets/vendor/prettify/prettify-min.js"></script>
413
<script>prettyPrint();</script>