Bird
Raised Fist0
Vueframework~20 mins

Key modifiers for keyboard events in Vue - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Keyboard Event Mastery in Vue
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when you use the .ctrl modifier in Vue keyboard events?
Consider a Vue 3 component with a button that listens for a keydown event with the .ctrl modifier. What behavior does the button exhibit when clicked with and without pressing the Control key?
Vue
<template>
  <button @keydown.ctrl="handleKey">Press Ctrl + Key</button>
</template>

<script setup>
function handleKey() {
  alert('Control key pressed with another key!');
}
</script>
AThe alert shows only if the Control key is NOT pressed.
BThe alert shows on every key press regardless of the Control key.
CThe alert only shows if the Control key is held down during the key press.
DThe alert never shows because .ctrl is not a valid modifier.
Attempts:
2 left
💡 Hint
Think about what the .ctrl modifier does in Vue event handling.
📝 Syntax
intermediate
2:00remaining
Which Vue key modifier syntax correctly listens for the Escape key?
You want to run a method when the Escape key is pressed on an input element. Which option uses the correct Vue key modifier syntax?
Vue
<template>
  <input @keydown.???="onEscape" />
</template>
A@keydown.escape="onEscape"
B@keydown.esc="onEscape"
C@keydown.Escape="onEscape"
D@keydown.escKey="onEscape"
Attempts:
2 left
💡 Hint
Vue key modifiers use lowercase names matching the official key names.
🔧 Debug
advanced
2:00remaining
Why does this Vue keyboard event handler not trigger on Shift + Enter?
Given this Vue template, why does pressing Shift + Enter not call the submitForm method?
Vue
<template>
  <textarea @keydown.enter.shift="submitForm"></textarea>
</template>

<script setup>
function submitForm() {
  alert('Form submitted');
}
</script>
AVue does not support multiple key modifiers on the same event.
BThe order of modifiers matters; .shift should come before .enter.
CThe correct syntax is @keydown.shift.enter, not @keydown.enter.shift.
DThe code works correctly; the issue is likely browser or focus related.
Attempts:
2 left
💡 Hint
Try testing the code in a browser and check if the event fires.
❓ state_output
advanced
2:00remaining
What is the value of message after pressing Alt + A in this Vue component?
Analyze the following Vue component. What will message be after the user presses Alt + A?
Vue
<template>
  <input @keydown.alt.a="updateMessage" />
  <p>{{ message }}</p>
</template>

<script setup>
import { ref } from 'vue'
const message = ref('')
function updateMessage() {
  message.value = 'Alt + A pressed'
}
</script>
A"Alt + A pressed"
B"" (empty string)
Cundefined
D"Alt pressed"
Attempts:
2 left
💡 Hint
The .alt and .a modifiers require both keys pressed together.
🧠 Conceptual
expert
2:00remaining
Which Vue key modifier prevents the event from propagating to parent elements?
You want to stop a keyboard event from bubbling up to parent elements in Vue. Which key modifier achieves this?
A.self
B.stop
C.prevent
D.capture
Attempts:
2 left
💡 Hint
Think about event propagation and how to stop it.

Practice

(1/5)
1. What does the Vue key modifier @keyup.enter do in a template?
easy
A. It triggers the event handler only when the Enter key is released.
B. It triggers the event handler on any key press.
C. It triggers the event handler only when the Escape key is pressed.
D. It triggers the event handler when the mouse is clicked.

Solution

  1. Step 1: Understand the key modifier syntax

    The @keyup.enter listens for the keyup event but only activates if the Enter key was released.
  2. Step 2: Compare with other keys and events

    Other keys like Escape or mouse clicks are not triggered by this modifier.
  3. Final Answer:

    It triggers the event handler only when the Enter key is released. -> Option A
  4. Quick Check:

    Key modifier .enter = Enter key only [OK]
Hint: Key modifiers match specific keys like .enter for Enter key [OK]
Common Mistakes:
  • Confusing keyup with keydown events
  • Thinking it triggers on any key press
  • Mixing Enter with Escape key
2. Which of the following is the correct Vue syntax to listen for the Escape key on a keydown event?
easy
A. @keydown.escKey
B. @keydown.escape
C. @keydown.Escape
D. @keydown.esc

Solution

  1. Step 1: Recall Vue key modifier names

    Vue uses .esc as the official key modifier for the Escape key.
  2. Step 2: Check syntax correctness

    The correct syntax is lowercase @keydown.esc. Other variants like .escape or capitalized forms are invalid.
  3. Final Answer:

    @keydown.esc -> Option D
  4. Quick Check:

    Escape key modifier = .esc [OK]
Hint: Use official Vue key names like .esc, not full words [OK]
Common Mistakes:
  • Using .escape instead of .esc
  • Capitalizing key modifiers
  • Adding extra words like .escKey
3. Given this Vue template code:
<input @keydown.ctrl.enter="submit" />

What happens when the user presses Ctrl + Enter?
medium
A. The submit method is called on any Ctrl key press.
B. The submit method is called on any Enter key press.
C. The submit method is called only if Ctrl and Enter are pressed together.
D. The submit method is never called because the syntax is invalid.

Solution

  1. Step 1: Understand combined key modifiers

    The @keydown.ctrl.enter listens for the Enter key pressed while Ctrl is held down.
  2. Step 2: Confirm event triggering condition

    The method triggers only when both Ctrl and Enter keys are pressed simultaneously, not separately.
  3. Final Answer:

    The submit method is called only if Ctrl and Enter are pressed together. -> Option C
  4. Quick Check:

    Combined keys like .ctrl.enter require both keys pressed [OK]
Hint: Multiple key modifiers mean all keys must be pressed together [OK]
Common Mistakes:
  • Thinking it triggers on Enter alone
  • Assuming Ctrl alone triggers it
  • Believing syntax is invalid
4. Identify the error in this Vue event listener:
<input @keyup.shift.enter="submit" />
medium
A. The event should be @keydown, not @keyup for Shift + Enter.
B. There is no error; this syntax is valid.
C. The correct modifier is .enter.shift, order matters.
D. The key modifier .shift.enter is invalid; modifiers must be separated by dots.

Solution

  1. Step 1: Check Vue key modifier chaining rules

    Vue allows chaining multiple key modifiers separated by dots in any order, so .shift.enter is valid.
  2. Step 2: Confirm event type and usage

    Using @keyup with .shift.enter is valid and triggers when Shift and Enter are released together.
  3. Final Answer:

    There is no error; this syntax is valid. -> Option B
  4. Quick Check:

    Multiple key modifiers chained with dots are valid [OK]
Hint: Order of key modifiers does not matter; chaining with dots is correct [OK]
Common Mistakes:
  • Thinking modifier order matters
  • Confusing keyup and keydown necessity
  • Assuming .shift.enter is invalid
5. You want to trigger a method only when the user presses Ctrl + Shift + S to save a form. Which Vue event listener is correct?
hard
A. @keydown.ctrl.shift.s="saveForm"
B. @keydown.shift.ctrl.s="saveForm"
C. @keydown.s.ctrl.shift="saveForm"
D. @keydown.save.ctrl.shift="saveForm"

Solution

  1. Step 1: Understand key modifier chaining

    Vue allows chaining key modifiers in any order, but the key itself must be last, so .ctrl.shift.s is correct.
  2. Step 2: Validate each option

    @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.
  3. Step 3: Choose the best standard syntax

    @keydown.ctrl.shift.s="saveForm" is the most standard and clear syntax.
  4. Final Answer:

    @keydown.ctrl.shift.s="saveForm" -> Option A
  5. Quick Check:

    Key modifiers chain with keys last; .ctrl.shift.s is correct [OK]
Hint: Put the key last, modifiers before it chained by dots [OK]
Common Mistakes:
  • Placing the key before modifiers
  • Using non-existent modifiers like .save
  • Assuming modifier order matters strictly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard