Skip to content

Commit d9a94ea

Browse files
feat(question): add Key Modifiers (#233)
* feat(question): add Key Modifiers Co-authored-by: webfansplz <308241863@qq.com>
1 parent 5bcecb9 commit d9a94ea

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<template>
2+
<!-- Add key modifiers made this will fire even if Alt or Shift is also pressed -->
3+
<button @click="onClick1">A</button>
4+
5+
<!-- Add key modifiers made this will only fire when Shift and no other keys are pressed -->
6+
<button @click="onCtrlClick">A</button>
7+
8+
<!-- Add key modifiers made this will only fire when no system modifiers are pressed -->
9+
<button @click="onClick2">A</button>
10+
</template>
11+
12+
<script setup>
13+
function onClick1(){
14+
console.log('onClick1')
15+
}
16+
function onCtrlClick(){
17+
console.log('onCtrlClick')
18+
}
19+
function onClick2(){
20+
console.log('onClick2')
21+
}
22+
</script>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!--info-header-start-->
2+
<!--info-header-end-->
3+
4+
5+
When listening for keyboard events, we often need to check for specific keys. Vue allows adding key modifiers for v-on or @ when listening for key events,e.g. :
6+
7+
```vue
8+
<!-- only call `vm.submit()` when the `key` is `Enter` -->
9+
<input @keyup.enter="submit" />
10+
```
11+
12+
For this challenges,we'll try something about it,let's go :
13+
14+
```vue
15+
<template>
16+
<!-- Add key modifiers made this will fire even if Alt or Shift is also pressed -->
17+
<button @click="onClick1">A</button>
18+
19+
<!-- Add key modifiers made this will only fire when Shift and no other keys are pressed -->
20+
<button @click="onCtrlClick">A</button>
21+
22+
<!-- Add key modifiers made this will only fire when no system modifiers are pressed -->
23+
<button @click="onClick2">A</button>
24+
</template>
25+
```
26+
27+
28+
<!--info-footer-start-->
29+
<!--info-footer-end-->
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!--info-header-start-->
2+
<!--info-header-end-->
3+
4+
5+
在监听键盘事件时,我们经常需要检查特定的按键。Vue 允许为 v-on 或者 @ 在监听键盘事件时添加按键修饰符:,例如:
6+
7+
``vue
8+
<!-- 只有在 `key` 是 `Enter` 时调用 `vm.submit()` -->
9+
<input @keyup.enter="submit" />
10+
````````
11+
12+
在这个挑战中,我们将尝试它,让我们开始吧:
13+
14+
```vue
15+
<template>
16+
<!-- 添加按键修饰符让即使 Alt 或 Shift 被一同按下时也会触发 -->
17+
<button @click="onClick1">A</button>
18+
19+
<!-- 添加按键修饰符让有且只有 Shift 被按下的时候才触发 -->
20+
<button @click="onCtrlClick">A</button>
21+
22+
<!-- 添加按键修饰符让没有任何系统修饰符被按下的时候才触发 -->
23+
<button @click="onClick2">A</button>
24+
</template>
25+
26+
```
27+
28+
29+
<!--info-footer-start-->
30+
<!--info-footer-end-->
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
difficulty: medium
2+
title: Key Modifiers
3+
tags: Event Handling
4+
author:
5+
github: webfansplz
6+
name: webfansplz
7+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
difficulty: medium
2+
title: 按键修饰符
3+
tags: Event Handling
4+
author:
5+
github: webfansplz
6+
name: webfansplz
7+

0 commit comments

Comments
 (0)