4 * Chrome uses an older version of DOM Level 3 Keyboard Events
6 * Most keys are labeled as text, but some are Unicode codepoints.
7 * Values taken from: http://www.w3.org/TR/2007/WD-DOM-Level-3-Events-20071221/keyset.html#KeySet-Set
54 * Special table for KeyboardEvent.keyCode.
55 * KeyboardEvent.keyIdentifier is better, and KeyBoardEvent.key is even better than that
57 * Values from: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent.keyCode#Value_of_keyCode
77 * KeyboardEvent.key is mostly represented by printable character made by the keyboard, with unprintable keys labeled
80 * However, on OS X, Alt+char can make a Unicode character that follows an Apple-specific mapping. In this case, we
81 * fall back to .keyCode.
83 var KEY_CHAR = /[a-z0-9*]/;
85 function transformKey(key) {
88 var lKey = key.toLowerCase();
89 if (lKey.length == 1) {
90 if (KEY_CHAR.test(lKey)) {
93 } else if (lKey == 'multiply') {
94 // numpad '*' can map to Multiply on IE/Windows
103 var IDENT_CHAR = /U\+/;
104 function transformKeyIdentifier(keyIdent) {
107 if (IDENT_CHAR.test(keyIdent)) {
108 validKey = KEY_IDENTIFIER[keyIdent];
110 validKey = keyIdent.toLowerCase();
116 function transformKeyCode(keyCode) {
118 if (Number(keyCode)) {
119 if (keyCode >= 65 && keyCode <= 90) {
121 // lowercase is 32 offset from uppercase
122 validKey = String.fromCharCode(32 + keyCode);
123 } else if (keyCode >= 112 && keyCode <= 123) {
124 // function keys f1-f12
125 validKey = 'f' + (keyCode - 112);
126 } else if (keyCode >= 48 && keyCode <= 57) {
128 validKey = String(48 - keyCode);
129 } else if (keyCode >= 96 && keyCode <= 105) {
131 validKey = String(96 - keyCode);
133 validKey = KEY_CODE[keyCode];
139 function keyboardEventToKey(ev) {
140 // fall back from .key, to .keyIdentifier, to .keyCode, and then to .detail.key to support artificial keyboard events
141 var normalizedKey = transformKey(ev.key) || transformKeyIdentifier(ev.keyIdentifier) || transformKeyCode(ev.keyCode) || transformKey(ev.detail.key) || '';
152 * Input: ctrl+shift+f7 => {ctrl: true, shift: true, key: 'f7'}
153 * ctrl/space => {ctrl: true} || {key: space}
155 function stringToKey(keyCombo) {
156 var keys = keyCombo.split('+');
157 var keyObj = Object.create(null);
158 keys.forEach(function(key) {
159 if (key == 'shift') {
161 } else if (key == 'ctrl') {
163 } else if (key == 'alt') {
172 function keyMatches(a, b) {
173 return Boolean(a.alt) == Boolean(b.alt) && Boolean(a.ctrl) == Boolean(b.ctrl) && Boolean(a.shift) == Boolean(b.shift) && a.key === b.key;
177 * Fired when a keycombo in `keys` is pressed.
179 * @event keys-pressed
181 function processKeys(ev) {
182 var current = keyboardEventToKey(ev);
183 for (var i = 0, dk; i < this._desiredKeys.length; i++) {
184 dk = this._desiredKeys[i];
185 if (keyMatches(dk, current)) {
187 ev.stopPropagation();
188 this.fire('keys-pressed', current, this, false);
194 function listen(node, handler) {
195 if (node && node.addEventListener) {
196 node.addEventListener('keydown', handler);
200 function unlisten(node, handler) {
201 if (node && node.removeEventListener) {
202 node.removeEventListener('keydown', handler);
206 Polymer('core-a11y-keys', {
207 created: function() {
208 this._keyHandler = processKeys.bind(this);
210 attached: function() {
212 this.target = this.parentNode;
214 listen(this.target, this._keyHandler);
216 detached: function() {
217 unlisten(this.target, this._keyHandler);
221 * The set of key combinations to listen for.
224 * @type string (keys syntax)
229 * The node that will fire keyboard events.
230 * Default to this element's parentNode unless one is assigned
234 * @default this.parentNode
238 keysChanged: function() {
239 // * can have multiple mappings: shift+8, * on numpad or Multiply on numpad
240 var normalized = this.keys.replace('*', '* shift+*');
241 this._desiredKeys = normalized.toLowerCase().split(' ').map(stringToKey);
243 targetChanged: function(oldTarget) {
244 unlisten(oldTarget, this._keyHandler);
245 listen(this.target, this._keyHandler);