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
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!
Copy file name to clipboardExpand all lines: src/v2/guide/events.md
+16-24Lines changed: 16 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -223,24 +223,28 @@ The `.passive` modifier is especially useful for improving performance on mobile
223
223
224
224
## Key Modifiers
225
225
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:
227
227
228
228
```html
229
-
<!-- only call `vm.submit()` when the `keyCode` is 13-->
230
-
<inputv-on:keyup.13="submit">
229
+
<!-- only call `vm.submit()` when the `key` is PageDown-->
230
+
<input@keyup.page-down="onPageDown">
231
231
```
232
232
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'`.
234
234
235
-
```html
236
-
<!-- same as above -->
237
-
<inputv-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
+
<pclass="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>
238
240
239
-
<!-- also works for shorthand -->
240
-
<input@keyup.enter="submit">
241
+
Using `keyCode` attributes is also permitted:
242
+
243
+
```html
244
+
<inputv-on:keyup.13="submit">
241
245
```
242
246
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:
244
248
245
249
-`.enter`
246
250
-`.tab`
@@ -252,27 +256,15 @@ Here's the full list of key modifier aliases:
252
256
-`.left`
253
257
-`.right`
254
258
259
+
<pclass="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
+
255
261
You can also [define custom key modifier aliases](../api/#keyCodes) via the global `config.keyCodes` object:
256
262
257
263
```js
258
264
// enable `v-on:keyup.f1`
259
265
Vue.config.keyCodes.f1=112
260
266
```
261
267
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
-
<pclass="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>
0 commit comments