0
0
Vueframework~20 mins

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

Choose your learning style9 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.