Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/keyup-modifier-timing.md
1---2title: System Modifier Keys Must Be Held During keyup Events3impact: MEDIUM4impactDescription: Modifier keys (ctrl, alt, shift, meta) behave differently with keyup - they must be held when the key is released5type: gotcha6tags: [vue3, events, keyboard, modifiers, keyup, shortcuts]7---89# System Modifier Keys Must Be Held During keyup Events1011**Impact: MEDIUM** - When using system modifier keys (`.ctrl`, `.alt`, `.shift`, `.meta`) with `keyup` events, the modifier must still be pressed when the other key is released. Releasing the modifier key first will not trigger the event, causing keyboard shortcuts to appear broken.1213## Task Checklist1415- [ ] Understand that `@keyup.ctrl` requires Ctrl to be held while releasing another key16- [ ] Consider using `keydown` instead of `keyup` for modifier key combinations17- [ ] Use `.exact` when you need precise modifier key control18- [ ] Test keyboard shortcuts with proper key release order1920**Incorrect:**21```html22<!-- WRONG: Expecting this to fire when Ctrl is released -->23<template>24<input @keyup.ctrl="onCtrlRelease" />25<!-- This does NOT fire when you just release Ctrl! -->26<!-- It fires when you release ANY key while holding Ctrl -->27</template>28```2930```html31<!-- WRONG: Misunderstanding keyup.ctrl behavior -->32<template>33<div @keyup.ctrl="handleShortcut">34<!-- User presses Ctrl+S, releases Ctrl first, then S -->35<!-- Event does NOT fire because Ctrl wasn't held during S release -->36</div>37</template>38```3940**Correct:**41```html42<!-- CORRECT: User must hold Ctrl while releasing another key -->43<template>44<input @keyup.ctrl.s="saveDocument" />45<!-- User presses Ctrl+S, then releases S while holding Ctrl -->46<!-- Event fires correctly -->47</template>4849<script setup>50function saveDocument(event) {51event.preventDefault()52// Save logic here53}54</script>55```5657```html58<!-- CORRECT: Use keydown for more intuitive modifier behavior -->59<template>60<div @keydown.ctrl.s="saveDocument">61<!-- keydown fires immediately when both keys are pressed -->62<!-- More intuitive for keyboard shortcuts -->63</div>64</template>65```6667```html68<!-- CORRECT: Use .exact for precise modifier control -->69<template>70<!-- Only fires when ONLY Ctrl is pressed (no Shift, Alt, etc.) -->71<button @click.ctrl.exact="onCtrlClick">Ctrl+Click Only</button>7273<!-- Fires with no system modifiers at all -->74<button @click.exact="onPlainClick">Plain Click Only</button>75</template>76```7778## How System Modifiers Work with keyup7980```javascript81// Timeline of Ctrl+S keydown:82// 1. User presses Ctrl (keydown fires)83// 2. User presses S while holding Ctrl (keydown fires)8485// Timeline of Ctrl+S keyup:86// 3. User releases S while holding Ctrl (keyup.ctrl.s fires!)87// 4. User releases Ctrl (keyup fires, but not keyup.ctrl.s)8889// Common mistake:90// 3. User releases Ctrl first (nothing fires for our handler)91// 4. User releases S (keyup.s fires, but not keyup.ctrl.s)92```9394## System Modifier Keys9596```html97<!-- Available system modifiers -->98<input @keyup.ctrl="..." /> <!-- Ctrl key -->99<input @keyup.alt="..." /> <!-- Alt key (Option on Mac) -->100<input @keyup.shift="..." /> <!-- Shift key -->101<input @keyup.meta="..." /> <!-- Cmd on Mac, Windows key on PC -->102```103104## The .exact Modifier105106```html107<!-- Different .exact behaviors -->108109<!-- Fires even if Shift/Alt are also pressed -->110<button @click.ctrl="onClick">Ctrl + any other modifiers</button>111112<!-- Fires ONLY when Ctrl alone is pressed -->113<button @click.ctrl.exact="onClick">Ctrl only, no other modifiers</button>114115<!-- Fires ONLY when no system modifiers are pressed -->116<button @click.exact="onClick">No modifiers allowed</button>117```118119## Best Practice: Prefer keydown for Shortcuts120121```html122<template>123<div124tabindex="0"125@keydown.ctrl.s.prevent="save"126@keydown.ctrl.z.prevent="undo"127@keydown.ctrl.shift.z.prevent="redo"128>129<!-- keydown is more reliable for keyboard shortcuts -->130<!-- Add .prevent to stop browser default (e.g., save dialog) -->131</div>132</template>133```134135## Reference136- [Vue.js Event Handling - Key Modifiers](https://vuejs.org/guide/essentials/event-handling.html#key-modifiers)137- [Vue.js Event Handling - System Modifier Keys](https://vuejs.org/guide/essentials/event-handling.html#system-modifier-keys)138