You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+6-1Lines changed: 6 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,11 +23,16 @@ The following lists show the changes to the library grouped by domain.
23
23
24
24
* changing [`ally.is.focusRelevant`][ally/is/focus-relevant] and [`ally.is.focusable`][ally/is/focusable] to regard `<keygen>` and `<embed>` focus-relevant but *not* focusable - [issue #82](https://github.com/medialize/ally.js/issues/82)
25
25
* changing [`ally.is.validArea`][ally/is/valid-area] to properly handle `<area href="…">` vs. `<area>` - [issue #72](https://github.com/medialize/ally.js/issues/72)
26
-
* changing [`ally.is.focusRelevant`][ally/is/focus-relevant] to properly handle `<object type="application/x-shockwave-flash">` in IE9 - [issue#71](https://github.com/medialize/ally.js/issues/71)
26
+
* changing [`ally.is.focusRelevant`][ally/is/focus-relevant] to properly handle `<object type="application/x-shockwave-flash">` in IE9 - [Issue#71](https://github.com/medialize/ally.js/issues/71)
27
27
* fixing [`ally.query.tabsequence`][ally/query/tabsequence] to return `<area>` elements at the correct position - [issue #5](https://github.com/medialize/ally.js/issues/5)
28
28
* fixing [`ally.query.tabsequence`][ally/query/tabsequence] to properly sort within Shadow DOM - [issue #6](https://github.com/medialize/ally.js/issues/6)
29
29
* refactoring [`ally.query.tabsequence`][ally/query/tabsequence] to extract `util/merge-dom-order` and `util/sort-dom-order`
30
30
31
+
#### Keyboard support
32
+
33
+
* changing [`ally.when.key`][ally/when/key] to handle modifier keys and respect `context` and `filter` options - [issue #59](https://github.com/medialize/ally.js/issues/59)
34
+
* changing [`ally.map.keycode`][ally/map/keycode] to provide alphanumeric keys and aliasing
35
+
31
36
#### Internals
32
37
33
38
* changing `ally.is.*` to work with other documents (e.g. iframes) - [issue #78](https://github.com/medialize/ally.js/issues/78)
*`v#master` added `_alias` to resolve multiple keyCodes for the same logical key.
74
+
75
+
76
+
## Notes
77
+
78
+
***NOTE:** The key `meta` is known by different keyCodes: `91`, `92`, `93`, `224` - which `ally.map.keycodes.alias.91` helps to resolve. The same is true for numeric keys (0-9) and their counterparts on the numblock.
79
+
80
+
40
81
## Related resources
41
82
83
+
*[`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) may help deal with this mess in the future.
Copy file name to clipboardExpand all lines: docs/api/when/key.md
+63-8Lines changed: 63 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,49 @@ Executes a callback when a given key has been pressed.
10
10
11
11
## Description
12
12
13
-
This is a convenience API to avoid adding and removing keyboard event handlers and having to filter for specific keys in application code. Callbacks are executed on `keydown`.
13
+
This is a convenience API to avoid adding and removing keyboard event handlers and having to filter for specific keys in application code. Callbacks are executed synchronously while handling [`keydown`](https://developer.mozilla.org/en-US/docs/Web/Events/keydown) events to maintain the ability to [`event.preventDefault()`](https://developer.mozilla.org/en/docs/Web/API/Event/preventDefault).
14
+
15
+
Keyboard events are dispatched to the currently focused element (`document.activeElement`). This allows us to handle keyboard events only when the user is engaged in a particular widget.
16
+
17
+
### Key binding syntax
18
+
19
+
In order to easily register keyboard events including modifier keys, `ally.when.key` understands the following `<key-binding>` syntax:
20
+
21
+
|`<key-binding>`| primary key | keyCode | alt | ctrl | meta | shift |
|`?shift+ctrl+enter`| <kbd>Enter</kbd> | 13 | no | yes | no | ? |
29
+
|`enter shift+8`| <kbd>Enter</kbd> | 13 | no | no | no | no |
30
+
|*(continued)*| <kbd>Backspace</kbd> | 8 | no | no | no | yes |
31
+
32
+
Legend: `no` means the modifier key is not pressed, `yes` means the modifier key is pressed, `?` means the state of the modifier key is ignored.
33
+
34
+
The `<key-binding>` syntax is defined by the following [EBNF](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form) grammar, with `property of map/keycode` referring to the property names of [`ally.map.keycode`](../map/keycode.md):
The modifier keys may have different names/symbols depending on operating system and physical keyboard:
49
+
50
+
| modifier key name | physical key on keyboard |
51
+
|-------------------|--------------------------|
52
+
| alt | <kbd>Alt</kbd>, <kbd>Option</kbd> or<kbd> ⌥</kbd> on Mac) |
53
+
| ctrl | <kbd>Ctrl</kbd>, <kbd>⌃</kbd> on Mac |
54
+
| meta | <kbd>Meta</kbd>, <kbd> ⌘</kbd> or <kbd></kbd> or <kbd>Command</kbd> on Mac, <kbd>⊞</kbd> or <kbd>Windows</kbd> on Windows |
55
+
| shift | <kbd>Shift</kbd>, <kbd>⇧</kbd> on Mac |
14
56
15
57
16
58
## Usage
@@ -33,19 +75,23 @@ handle.disengage();
33
75
34
76
### Arguments
35
77
36
-
The method accepts an object of `<key>: <callback>` mappings. See [Callback Signature](#Callback-Signature) for details
37
-
38
-
`<key>`s can be specified as a numeric code ([`keyCode`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode)) e.g. `13` for the <kbd>Enter</kbd> key, or as the string name `enter` which is resolved to the numeric code internally using [`ally.map.keycode`](../map/keycode.md). Multiple `<key>: <callback>` combinations can be passed.
78
+
| Name | Type | Default | Description |
79
+
| ---- | ---- | ------- | ----------- |
80
+
| context |[`<selector>`](../concepts.md#Selector)|[`documentElement`](https://developer.mozilla.org/en-US/docs/Web/API/Document/documentElement)| The scope of the DOM in which keyboard events will be processed. The first element of a collection is used. |
81
+
| filter |[`<selector>`](../concepts.md#Selector)|`null`| The elements and descendants to exclude when processing keyboard events. |
82
+
|[`<key-binding>`](#Key-binding-syntax)| function |*required*| Mapping of `<key-binding>` to callback function. See [Callback Signature](#Callback-Signature) for details. This argument is expected one or more times. |
39
83
40
84
### Returns
41
85
42
86
A [`<service>`](../concepts.md#Service) interface, providing the `handle.disengage()` method to stop the service.
43
87
44
88
### Throws
45
89
46
-
*[`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) if `<ke>` is neither numeric, nor found in [`ally.map.keycode`](../map/keycode.md).
90
+
*[`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) if no `<key-binding>: <callback>` combinations were passed.
91
+
*[`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) if `<key-binding>` does not resolve to a keyCode.
92
+
*[`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) if `<key-binding>` contains illegal modifier names.
47
93
*[`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) if `<callback>` is not a `function`.
48
-
*[`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) when no `<key>: <callback>` combinations were passed.
94
+
49
95
50
96
## Callback signature
51
97
@@ -54,27 +100,36 @@ A [`<service>`](../concepts.md#Service) interface, providing the `handle.disenga
54
100
| event |[`KeyboardEvent`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent)|*required*| The original `keydown` event. |
55
101
| disengage | function |*required*| The service's `handle.disengage()` method. |
56
102
57
-
The callback is executed in the context of `document.documentElement` (that's where `this` inside the callback points to). The callback is passed the `handle.disengage()` method to allow simplified "execute once" behaviors. The callback's return value is ignored.
103
+
The callback is executed in the context of `context` (that's where `this` inside the callback points to). The callback is passed the `handle.disengage()` method to allow simplified "execute once" behaviors. The callback's return value is ignored.
*`v#master` introduced the extended `<key-binding>` syntax (thereby changing the default modifier key behavior).
114
+
*`v#master` introduced the options `context` and `filter`.
115
+
116
+
65
117
## Notes
66
118
67
119
***NOTE:** Firefox has a long standing issue with keyboard events propagating to the document while browser UI like autocomplete is being interacted with [Gecko 286933](https://bugzilla.mozilla.org/show_bug.cgi?id=286933).
120
+
***WARNING:** The callback for the `<key-binding>``space` only executes if *no* modifier key was pressed. In order to make the callback execute regardless of modifier keys, use the `<key-binding>``*+space`.
121
+
***NOTE:** The modifiers `alt`, `ctrl`, `meta` usually engage system-level or browser-level functionality. Do not use these modifiers lightly. For example `alt+a` prints the letter `å` on a Mac with German keyboard layout, `meta+q` exits the application and `meta+space` engages Spotlight.
68
122
69
123
70
124
## Related resources
71
125
72
126
*[`ally.map.keycode`](../map/keycode.md) used for resolving named keys
127
+
* The `<key-binding>` syntax is inspired by [PolymerElements/iron-a11y-keys](https://github.com/PolymerElements/iron-a11y-keys#grammar)
0 commit comments