Hello and welcome back.
Let's add in some event, listeners to listen for a keyboard presses so that we can take action on those
user interactions.
So using the document object, we're adding an event listener to the entire document.
You could also add it to the container.
So whenever the user has selected the container, but in this case, we're going to attach it to the
document.
So we're listening for the event of key down and if key down, if any of the keys are pressed on the
keyboard, then this is going to invoke this key down function.
You can see that we can console log out that event object.
So I'm pressing the space.
You can see that there's quite a bit of information that gets passed through the vent object.
You can see which key got pressed within the key code as well as within the code.
So it's going to be the same thing.
And typically we do use the key codes as this is a more exact way to get what the key value is.
You can also use which so that's going to also have that same key code.
So for the space, the key code is thirty two for going left.
It's going to be thirty seven and four going right.
It's going to be thirty nine.
So we're going to listen on those events and we're going to grab that key value.
So getting the key value object where we're going to use that event object and then key code in order
to retrieve that key information.
And then we can also output in the console so that we can actually see which keys we're not passing,
that we're not getting that full object in the console anymore.
It's just going to be the same key values.
So it's left.
That's right.
And that's space.
And there's also up, which is thirty eight.
So we're going to attach the firing on 32 and 38 left is going to be thirty seven and right is going
to be thirty nine.
And within the player object, we could add in those and we can also set up another main object in order
to control the key press so we can say key value and then this can just be an open object.
And then as we create those, we can create them down here.
So checking to see if the key value is equal to 37.
And if it is there, we're going to take the key value or just call it key key V with an uppercase V
object and assigning left.
So setting that left value to true.
And then we're going to check to see if the key value is 39.
And that means that we're setting the right to.
True.
So this is going to give us an indicator on which direction.
And we're also going to listen for the key up and then we're going to set these object values to false.
So listening for those key presses, and this can also be an elusive because if one of the keys is pressed,
if it's 37, then it's not going to be 39 or any of the other ones.
And also, let's check to see what if the key is pressed and if it's equal to 38 or if the key that's
pressed is equal to 32.
And if that's true, then we are going to fire so we can take the player fire value and set that to
be true.
So that means that the player has invoked and they fired and we'll take care of that in one of the upcoming
lessons.
So now we've got tracking those key V objects within the mean key object.
So now if I press any of the keys, you can see that they go and we get the boolean values, whether
they're true or not.
So there's another event listener that we need to add, and this is going to do the opposite.
So we've got key down and then whenever the player releases the key, we're going to do the same thing
where we're getting the key event.
And if it's key 37, we're going to set that to false.
If the key.
Right, and we're set that one to false.
And if this is lifted it up.
So we've got to have a different action for the key up.
We don't need to listen for the firing because we're only listening for the firing on the key down so
we can avoid it within that scenario.
So now, whenever I press any of the keys, you're going to see that when I press it down, it goes
true.
When I release, it goes false.
And then the next one, when I hit right, it goes true.
And when I release, it goes false.
So now we can because this is a global object, the key value is a global object.
When we're doing our animation, we can check to see what this key value is and apply the motion in
the movement up accordingly.
So go ahead and add this into your project.
And coming up next, we're going to create our animation frame and all of the key presses are going
to become more evident.
And we're actually going to be able to move our ship back and forth depending on what keys are being
pressed on the keyboard.
So that's coming up next.