-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
50 lines (35 loc) · 946 Bytes
/
script.js
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
// this '//' represent the stages
// get all keys
const keys = document.querySelectorAll(".key")
// play notes
function playNote(event) {
// keyCode
let audioKeyCode = getKeyCode(event);
// typed or pressed key
const key = document.querySelector(`.key[data-key="${audioKeyCode}"]`)
// if key exists
const cantFindAnyKey = !key
if(cantFindAnyKey) {
return;
}
// play audio
const audio = document.querySelector(`audio[data-key="${audioKeyCode}"]`)
audio.currentTime = 0;
audio.play()
}
function getKeyCode(event) {
let keyCode;
const isKeyboard = event.type === "keydown"
if(isKeyboard) {
keyCode = event.keyCode
} else {
keyCode = event.target.dataset.key
}
return keyCode
}
// click with mouse
keys.forEach(function(key) {
key.addEventListener("click", playNote)
})
// keyboard type
window.addEventListener("keydown", playNote)