|
| 1 | +include ../_util-fns |
| 2 | + |
| 3 | +a( id='top' ) |
| 4 | + |
| 5 | +:marked |
| 6 | + With Angular's powerful event-binding syntax, we can bind to any native DOM |
| 7 | + (Document Object Model) event by wrapping the **event type** in parentheses. |
| 8 | + For example, if we wanted to bind to the `focus` or `click` event, we would |
| 9 | + add an element binding for `(focus)` or `(click)`, respectively. The same can |
| 10 | + be done for `(keydown)`, `(keyup)`, and `(keypress)` events. However, with |
| 11 | + key events, we generally want to do more than _just_ bind to the event |
| 12 | + — we want to respond to specific key combinations. Luckily, Angular's |
| 13 | + event-binding system can help us accomplish this as well. |
| 14 | + |
| 15 | +:marked |
| 16 | + When defining a `keydown` or `keyup` event-binding, we can provide additional |
| 17 | + settings that determine which keyboard events will cause our event handler to |
| 18 | + be invoked. In the event-binding syntax, these settings are provided as a |
| 19 | + dot-delimited suffix to the core event type. |
| 20 | + |
| 21 | +:marked |
| 22 | + So, for example, if we wanted to respond specifically to the `Enter` key in a |
| 23 | + `keydown` event, we would add the following element binding: |
| 24 | + `(keydown.Enter)`. And, if we wanted to respond to the increasingly-popular |
| 25 | + `Command+Enter` key combination, we would add the following element binding: |
| 26 | + `(keydown.Meta.Enter)`. |
| 27 | + |
| 28 | +:marked |
| 29 | + Any alpha-numeric character can be used in the event-binding as long as it |
| 30 | + doesn't break the HTML parser (Caution: `>` will break the parser). Special |
| 31 | + characters like the period and the space are made available via `Dot` and |
| 32 | + `Space`, respectively. In addition, [many control characters](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key#Key_values) |
| 33 | + like `ArrowLeft` and `ArrowRight` can be used as well. |
| 34 | + |
| 35 | +.l-sub-section |
| 36 | + :marked |
| 37 | + Commonly-used control characters include: |
| 38 | + |
| 39 | + * `Enter` |
| 40 | + * `Escape` |
| 41 | + * `ArrowLeft`, `ArrowUp`, `ArrowRight`, `ArrowDown` |
| 42 | + * `Tab` |
| 43 | + |
| 44 | +:marked |
| 45 | + The following modifier modifiers can be used (either on their own or in |
| 46 | + combination with other modifiers and characters): |
| 47 | + |
| 48 | +:marked |
| 49 | + * `Alt` |
| 50 | + * `Control` |
| 51 | + * `Meta` (`Command` key on the Mac, `Start` key on Windows) |
| 52 | + * `Shift` |
| 53 | + |
| 54 | +:marked |
| 55 | + None of the event-binding settings are case-sensitive. `Tab` is the same as |
| 56 | + `tab` and `A` is the same as `a`. However, the event settings have to match |
| 57 | + on all event conditions. This means that our event-bindings have to account |
| 58 | + for modifier keys if modifier keys are required in order to render the target |
| 59 | + character. For example: |
| 60 | + |
| 61 | +:marked |
| 62 | + * `keydown.a` codes for `a`. |
| 63 | + * `keydown.Shift.a` codes for `A` _(if the Shift key was depressed)_. |
| 64 | + * `keydown.Shift.?` codes for the question mark. |
| 65 | + |
| 66 | +:marked |
| 67 | + In the following code, we're attaching multiple key combination events to a |
| 68 | + single `input` and then logging the key values to a list: |
| 69 | + |
| 70 | +:marked |
| 71 | + **See the [live example](/resources/live-examples/cb-key-bindings/ts/plnkr.html)**. |
| 72 | + |
| 73 | ++makeExample( 'cb-key-bindings/ts/app/app.component.ts', 'component', 'app/app.component.ts' )(format='.') |
| 74 | + |
| 75 | +:marked |
| 76 | + When we run this demo and try some of the key combination bindings, we get |
| 77 | + the following output: |
| 78 | + |
| 79 | +figure.image-display |
| 80 | + img(src='/resources/images/cookbooks/key-bindings/key-bindings.gif' alt='Key Bindings') |
| 81 | + |
| 82 | +:marked |
| 83 | + When we use these key bindings, Angular doesn't alter the behavior of the |
| 84 | + keys in any way. As such, if we want to override the default behavior |
| 85 | + (such as preventing the `Tab` key from removing focus from an input), we |
| 86 | + have to call the `.preventDefault()` method on the associated event object. |
| 87 | + |
| 88 | +:marked |
| 89 | + At the time of this writing, these special key combination settings are only |
| 90 | + supported at the element level and only for the `keydown` and `keyup` events. |
| 91 | + Meaning, these cannot be used with the global `window:` or `document:` |
| 92 | + targets; and, they cannot be used with the `keypress` event. |
| 93 | + |
| 94 | +:marked |
| 95 | + [Back to top](#top) |
0 commit comments