Now if we wanted our web site to not just detect button presses but also to detect keyboard key presses 1
to activate the relevant drum, 2
say if I press the w key then it should activate this drum and if I press j then it should activate 3
the snare, 4
then how exactly do we do that? 5
Now instead of adding an event listener to listen for clicks we can listen to a key press event instead. 6
So we would write something like addEventLlistener("keypress"), and notice that the weird thing about 7
events is that they're named all in lower case, and it's a good idea to take a look at the docs whenever 8
you're trying to type them because it's pretty counter-intuitive. 9
So we're going to try and target this keypress event, and if there was a key press, then we'll simply 10
call an anonymous function which sends an alert that says "Key was pressed". 11
So but the problem is what exactly do we add this event listener to, right? 12
Because previously we added our event listener to all of the buttons, so that when the buttons get pressed 13
then the event listener gets triggered. 14
But in this case we're kind of waiting for the keyboard keys. 15
So how do we add an event listener to the keyboard? 16
Well you don't have to. 17
You can add an event listener to the entire document, so that the entire web page starts listening for 18
keyboard strokes. 19
So now let's hit save and let's refresh our page, and if I tap on any of the keys on my keyboard then 20
I get the alert that says "Key was pressed". 21
Now the next problem is how do we figure out which key was pressed 22
exactly. 23
Now remember how we said that when we add an event listener to an element then once the event happens 24
the element will trigger the function that's in the second parameter. 25
So in this case when a key press is detected it will trigger the function that tells the browser to 26
show an alert. 27
Now when that function gets triggered, there's also the option to pass in a parameter, 28
and we can call that parameter event or e, 29
but essentially what it allows us to do is it lets us tap into the event that triggered the event listener. 30
So instead of showing an alert, let's console.log this event and see what we get back. 31
So now if we refresh our page and we press any key then you can see that what's logged in here are the 32
keyboard events that triggered the event listener. 33
So if you expand this you can see that it actually gives you a whole bunch of information including 34
the key that was pressed to trigger the event, 35
other things such as the key code, the character code, and a whole bunch of other things. 36
So you can see that whenever I press any key on the keyboard this event tells me which key was pressed. 37
And that's kind of what we need in order to get our drum kit going, 38
right? 39
And if you look in the docs for the keypress events, then you can see that they have a whole bunch of 40
properties that we saw in the Chrome Developer Tools, and you can read about what each of them do by taking 41
a look at the MDN docs. 42
Now the key that we're most interested in is this one called key and this is the key value represented 43
by the event, 44
so which key on the keyboard got tapped essentially. 45
And this is what we need in order to figure out which sound to play. 46
So notice how we already have this switch statement which checks to see which key was passed in and 47
responds by playing the relevant sound. 48
Now the problem is that this is all inside the event listener for our button clicks. 49
So as a challenge I want you to create a function that takes a character and checks it against the switch 50
statement in order to play the correct sound, 51
and then we're going to call it inside the event listener for the key press as well as the 52
event listener for our drum buttons. 53
So pause the video and see if you can complete this challenge. 54
All right. 55
So the first thing we're going to do is we're going to create this brand new function and I'm simply 56
going to call it makeSound. And it's going to take a single parameter which is going to be the key. 57
Now inside this function I'm going to relocate our switch statement. 58
So I'm just going to cut it and then I'm going to paste it inside my function makeSound. Instead of 59
switching on the buttonInnerHTML, 60
I'm going to switch on this key, and I'm going to pass in that key by calling makeSound, passing in the 61
buttonInnerHTML, which is essentially the innerHTML of the button that got pressed. 62
But I'm also going to do the same thing down here. 63
So I'm going to get rid of the console.log, and instead we're going to call makeSound, and I'm going 64
to use the event.key. 65
So now I have one section of my code which is all about detecting button press and then I have another 66
part of my code which is all about detecting keyboard press, and if a button was pressed and then I check 67
the innerHTML of the button that got pressed and send that to makeSound in order to play the relevant 68
sound, 69
and if a key press was detected instead then I send the event.key, so the key property of the event. 70
So say if I press on the w button, then it's makeSound(w), and if I press on the w key on the keyboard it's 71
also makeSound(w), and they end up in here where we switch based on that key parameter. 72
And then we play the relevant sound depending on which key was passed in. 73
So let's save this and see if it works. 74
So first I'm going to check by clicking on my buttons. Seems to work. 75
And now I'm going to try and type in the characters. 76
Awesome, right? 77
So in this lesson we learned about how you can add event listeners to detect keyboard strokes and we 78
used the same method, addEventListener, 79
but we added it to the entire document instead of a specific button or a specific element. 80
And once that was detected then this function gets called passing in the event that triggered the key 81
press, and that event contains a property called key, which tells us which keyboard key was pressed. 82
And then we send that over to our function makeSound in order to make the sound that we want for that 83
key. 84
Now in order to fully understand this part of the code we have to understand how in Javascript we can 85
pass functions as parameters. 86
We can also have functions as callbacks which include things that we get back after the event has happened. 87
So in the next lesson we're going to explore some of those concepts in more detail so that we can really 88
get to grips with what exactly it is that we're writing here. 89
So for all of that and more, I'll see you on the next lesson.