Performance: Key modifiers for keyboard events
This affects interaction responsiveness and event handling efficiency during user keyboard input.
<template> <input @keydown.enter="handleEnter" /> </template> <script setup> function handleEnter() { // do something } </script>
<template> <input @keydown="handleKeydown" /> </template> <script setup> function handleKeydown(event) { if (event.key === 'Enter') { // do something } } </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual key check in handler | 1 event listener | 0 | 0 | [X] Bad |
| Vue key modifiers (@keydown.enter) | 1 event listener | 0 | 0 | [OK] Good |
@keyup.enter do in a template?@keyup.enter listens for the keyup event but only activates if the Enter key was released..enter = Enter key only [OK].esc as the official key modifier for the Escape key.@keydown.esc. Other variants like .escape or capitalized forms are invalid.<input @keydown.ctrl.enter="submit" />
@keydown.ctrl.enter listens for the Enter key pressed while Ctrl is held down.<input @keyup.shift.enter="submit" />
.shift.enter is valid.@keyup with .shift.enter is valid and triggers when Shift and Enter are released together..ctrl.shift.s is correct.@keydown.ctrl.shift.s="saveForm" correctly places the key s last. @keydown.s.ctrl.shift="saveForm" places the key in the middle, which is invalid. @keydown.save.ctrl.shift="saveForm" uses a non-existent modifier .save.@keydown.ctrl.shift.s="saveForm" is the most standard and clear syntax.Pass the test and this pattern moves unknown