Skip to content

Commit 2cd9bc5

Browse files
committed
added keylistener
1 parent bd3a78f commit 2cd9bc5

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

src/App.css

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@
22
#drum-machine{
33
display: flex;
44
height: 200px;
5-
width: 400px;
5+
width: 240px;
66
flex-flow: row wrap;
7+
78
}
9+
h5{
10+
flex: 1 100%;
11+
text-align: center;
12+
}
13+
814
.drum-pad{
915
width: 70px;
1016
height: 70px;
1117
margin: 5px;
1218
line-height: 70px;
1319
text-align: center;
20+
border-radius: 20%;
21+
1422
}
1523
.App-logo {
1624
height: 40vmin;
@@ -26,7 +34,7 @@
2634
cursor: grab;
2735
user-select: none;
2836
}
29-
.App-header {
37+
.App {
3038
background-color: #282c34;
3139
min-height: 100vh;
3240
display: flex;

src/Display.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33
function Display(props){
4-
return <p>{props.display}</p>
4+
return <h5>{props.display}</h5>
55
}
66

77

src/KeyPad.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,28 @@ import { useEffect, useState } from "react"
22

33

44
function KeyPad ({display,tone,id,src,keys}){
5-
const inActive = {background:"gray"};
6-
const active = {background:"tomato"}
5+
var randomColor = Math.floor(Math.random()*16777215).toString(16);
6+
const inActive = {background:"#"+randomColor};
7+
const active = {"background":"tomato","box-shadow":"2px 1px 2px wheat"}
78
const [style,setStyle]=useState(inActive);
89
const activate= ()=>{
9-
console.log("this is activate" + keys)
1010
document.getElementById(id).currentTime = 0;
1111
document.getElementById(id).play();
1212
setStyle(active);
1313
setTimeout(()=>setStyle(inActive),100);
1414
display(tone);
1515
}
16-
const handleKey =()=>{
17-
console.log("im handeling key press")
18-
document.getElementById(keys).play()
19-
}
20-
21-
2216

17+
const handleKey = (e)=>{
18+
if (e.key===keys){
19+
activate();
20+
}
21+
}
22+
useEffect(()=>{
23+
document.addEventListener("keydown",handleKey);
24+
return ()=>document.removeEventListener("keydown",handleKey);
25+
}
26+
)
2327

2428

2529

0 commit comments

Comments
 (0)