What are key modifiers?
Key modifiers are special modifiers for the v-on directive that let a handler run only when a specific key or key combination is pressed.
They are used with the keyup and keydown events.
In simple terms:
Key modifiers let you react only to the keys you need (for example Enter, Escape, arrows, Tab), without manually checking
event.keyin code.
The simplest example
html
<input @keyup.enter="sendMessage">The handler fires only if the user presses Enter.
Without modifiers, you would have to write:
js
onKeyPress(e) {
if (e.key === 'Enter') {
this.sendMessage()
}
}Main key modifiers
Frequently used:
html
@keyup.enter
@keyup.esc
@keyup.tab
@keyup.spaceArrows:
html
@keyup.up
@keyup.down
@keyup.left
@keyup.rightDeletion:
html
@keyup.delete <!-- works as both Backspace and Delete -->Combinations with system modifiers
html
@keyup.ctrl.enter="submit"
@keyup.shift.tab="backward"
@keyup.alt.s="save"This works because Vue checks:
event.ctrlKeyevent.shiftKeyevent.altKeyevent.metaKey(Cmd on Mac)
A useful case: submitting a form on Enter
html
<input @keyup.enter="submitForm">Dynamic key modifier
You can use a computed event name:
html
<input v-on:[eventName]="doSomething">But key modifiers only work with a direct key name, for example:
html
@keyup[eventName] Not allowedImportant (Vue 2 vs Vue 3)
- In Vue 2, there were
keyCodes(for example,keyCode: 13) - In Vue 3, they were removed, and modifiers work only through key names (
enter,esc)
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.