50 project 50 days practice
event-keycodes
Press any key to get the keyCode
<template>
<div class="event-keycodes">
<div class="insert">
<div class="key" v-if="firstOpen">Press any key to get the keyCode</div>
<div v-else>
<div class="key">
{{ key.key == " " ? "Space" : key.key }}
<small>event.key</small>
</div>
<div class="key">
{{ key.keyCode }}
<small>envent.keyCode</small>
</div>
<div class="key">
{{ key.code }}
<small>event.code</small>
</div>
</div>
</div>
</div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<script>
export default {
data() {
return {
firstOpen: true,
key: {
code: "",
keyCode: "",
key: "",
},
};
},
methods: {
keydown(key) {
this.firstOpen = false;
console.log(key);
this.key = key;
},
},
mounted() {
window.addEventListener("keydown", this.keydown);
},
destroyed() {
window.removeEventListener("keydown", this.keydown);
},
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<style scoped>
@import url("https://fonts.googleapis.com/css?family=Muli&display=swap");
* {
box-sizing: border-box;
}
.event-keycodes {
background-color: steelblue;
font-family: "Muli", sans-serif;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
height: 60vh;
overflow: hidden;
margin: 0;
}
.key {
border: 1px solid #999;
background-color: #9adbbe;
box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.1);
display: inline-flex;
align-items: center;
font-size: 20px;
font-weight: bold;
padding: 20px;
flex-direction: column;
margin: 10px;
min-width: 150px;
position: relative;
}
.key small {
position: absolute;
top: -24px;
left: 0;
text-align: center;
width: 100%;
color: #9adbbe;
font-size: 14px;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43