-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKeyEvents.html
More file actions
56 lines (49 loc) · 1.28 KB
/
KeyEvents.html
File metadata and controls
56 lines (49 loc) · 1.28 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="myBox"></div>
<script>
// eventListener = Listen for specific events to create interactive web pages
// events: keydown, keyup
// document.addEventListener(event, callback);
const myBox = document.getElementById("myBox")
const moveAmount = 10
let x = 0
let y = 0
document.addEventListener("keydown", (event) => {
myBox.textContent = "😲"
myBox.style.backgroundColor = "tomato"
})
document.addEventListener("keyup", (event) => {
myBox.textContent = "😀"
myBox.style.backgroundColor = "lightblue"
})
document.addEventListener("keydown", (event) => {
if (event.key.startsWith("Arrow")) {
event.preventDefault()
switch (event.key) {
case "ArrowUp":
y -= moveAmount
break
case "ArrowDown":
y += moveAmount
break
case "ArrowLeft":
x -= moveAmount
break
case "ArrowRight":
x += moveAmount
break
}
myBox.style.top = `${y}px`
myBox.style.left = `${x}px`
}
})
</script>
</body>
</html>