Lesson 5: Events
Try this activity to show events: Have Student A go out, ask him to close the door behind him, wait a few seconds, and then knock on the door (preferably, you want to do this without the rest of the class noticing). Another student is likely to hear the knock and open the door. If no one opens the door, you can ask someone to do so. Tell the students that knocking on the door is an event and the reaction to that event was opening the door. Ask them for other examples of events in their daily life.
Q: What comes to mind when you think of the word “EVENT” ?
A: An event is when something happens. Your birthday is an event and so is the first day of school. In coding, there are many kinds of events, but we will focus on events in which you click or press on something.
Q: What do you think an input is? What about an output?
A: Inputs are signals you send the computer and outputs are signals the computer sends to you. You can send signals to the computer using input devices. The computer sends signals to you using output devices.
Q: What are some input devices?
A: Input devices include a mouse, a keyboard, a touch-screen , and a microphone.
Q: What are some output devices?
A: Output devices include a computer screen (a monitor), speakers, and L.E.D. light indicators like the one that tells you if your battery is charging.
Q: Can you think of more input and output devices?
Let’s think about some examples of events in our daily life
Q: What is an Event?
A: Event – An action that causes something to happen. In this lesson, we will focus on the Keydown event. That is when you press keys on your keyboard. For example, if you press any of the letter keys on your keyboard, your keyboard will type that letter. But we will make the turtle do something whenever you press a key.
Create events for the arrow keys
First, you will need to find the arrow keys on your keyboard. You can see what they look like on the image below:
We will make it so that every time that you press one of these keys, the turtle will do something!
First, we need to find the Keydown event block. You can find it in the control category.
Check out the keydown blocks below.
Q: Which one of the blocks will activate when you press the Up arrow key?
A: The first block will activate when you press the X key, the second block when you press the 8 key, and the third block when you press the Up key!
Making the ‘KeyDown’ Block Do Something
To make your keydown block do anything, you must put another block inside of it. All the keydown block does is tell the turtle that it should do something when the key is pressed. Whichever blocks you put inside the keydown block are what the turtle will do.
Add two keydown blocks to your code.
Change the ‘X’ on the first one to ‘Up’.
Change the ‘X’ on the second one to ‘Down’
Find the blocks that make the turtle move forward and backward in the move section and add them to your keydowns. You can see them in the image below.
After you added the blocks, press the play button. When your code is playing, you can press the up and down arrow keys on your keyboard to make the turtle move forward and backward.
*IF THE TURTLE ISN’T MOVING AFTER YOU PRESS PLAY, TRY CLICKING ON YOUR TURTLE WITH YOUR MOUSE AND TRY AGAIN.
Q: How do you think we can make the turtle move left and right?
A: We will add two more keydowns for the left and right arrow keys.
Q: How do we make our turtle move faster?
A: We can add a speed block to our code and our turtle will move faster. You can add the speed block at the top of you code now
Add two more keydowns.
Change the ‘X’ on one of these new ones to ‘Left’
Change the ‘X’ on your other new keydown block to ‘Right’
Inside of these keydowns, we will add a block that turns the turtle, moves it forward, and then turns it back. Check out the code blocks on the image below:
Great! You can now move your turtle. Try changing the numbers for the FD and your speed. Have fun moving your turtle around.
What is an event?
Why do we need to be able to handle events in a program?
What are some other kinds of events that you can think of?