Skip to content

Commit 67766e4

Browse files
naclontsphanan
authored andcommitted
Update Key Modifiers to suggest KeyboardEvent.key (#1850)
Fixes [#1765](#1765). I left the "define custom key modifier aliases" section at the bottom, below the deprecated key codes information. Definitely open to changing / re-arranging as needed!
1 parent 42c313c commit 67766e4

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

src/v2/guide/events.md

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -223,24 +223,28 @@ The `.passive` modifier is especially useful for improving performance on mobile
223223

224224
## Key Modifiers
225225

226-
When listening for keyboard events, we often need to check for common key codes. Vue also allows adding key modifiers for `v-on` when listening for key events:
226+
When listening for keyboard events, we often need to check for specific keys. Vue allows adding key modifiers for `v-on` when listening for key events:
227227

228228
``` html
229-
<!-- only call `vm.submit()` when the `keyCode` is 13 -->
230-
<input v-on:keyup.13="submit">
229+
<!-- only call `vm.submit()` when the `key` is PageDown -->
230+
<input @keyup.page-down="onPageDown">
231231
```
232232

233-
Remembering all the `keyCode`s is a hassle, so Vue provides aliases for the most commonly used keys:
233+
In the above example, the handler will only be called if `$event.key` is equal to `'PageDown'`.
234234

235-
``` html
236-
<!-- same as above -->
237-
<input v-on:keyup.enter="submit">
235+
You can directly use any valid key names exposed via [`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) as modifiers by converting them to kebab-case.
236+
237+
### Key Codes
238+
239+
<p class="tip">The use of `keyCode` events [is deprecated](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode) and may not be supported in new browsers.</p>
238240

239-
<!-- also works for shorthand -->
240-
<input @keyup.enter="submit">
241+
Using `keyCode` attributes is also permitted:
242+
243+
``` html
244+
<input v-on:keyup.13="submit">
241245
```
242246

243-
Here's the full list of key modifier aliases:
247+
Vue provides aliases for the most commonly used key codes when necessary for legacy browser support:
244248

245249
- `.enter`
246250
- `.tab`
@@ -252,27 +256,15 @@ Here's the full list of key modifier aliases:
252256
- `.left`
253257
- `.right`
254258

259+
<p class="tip">A few keys (`.esc` and all arrow keys) have inconsistent `key` values in IE9, so these built-in aliases should be preferred if you need to support IE9.</p>
260+
255261
You can also [define custom key modifier aliases](../api/#keyCodes) via the global `config.keyCodes` object:
256262

257263
``` js
258264
// enable `v-on:keyup.f1`
259265
Vue.config.keyCodes.f1 = 112
260266
```
261267

262-
### Automatic Key Modifiers
263-
264-
> New in 2.5.0+
265-
266-
You can also directly use any valid key names exposed via [`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) as modifiers by converting them to kebab-case:
267-
268-
``` html
269-
<input @keyup.page-down="onPageDown">
270-
```
271-
272-
In the above example, the handler will only be called if `$event.key === 'PageDown'`.
273-
274-
<p class="tip">A few keys (`.esc` and all arrow keys) have inconsistent `key` values in IE9, their built-in aliases should be preferred if you need to support IE9.</p>
275-
276268
## System Modifier Keys
277269

278270
> New in 2.1.0+

0 commit comments

Comments
 (0)