The name whenKeyDown describes what kind of event you want to "listen" for, and since it is a method, we set it equal to a function that’s listening for a key on your keyboard to be pressed.
5. You’re almost done with the coding part of the project! Next, you and your students need to specify what happens when a specific key gets selected. When a user presses the up arrow on their keyboard, the video should get more pixelated. When they press the down arrow, it should get less pixelated.
The first step is to write a conditional in the whenKeyDown function. The most common conditional is an if-statement. Imagine if you're giving someone directions. You can say, if you see a red house, turn left. An if-statement is like that, but in a language your computer can read.
Have your students write an if-statement inside of their whenKeyDown function. This will look like:
movie.whenKeyDown = function(key) {
if (true){
//students will add code here soon!
}
};
Which key gets pressed will be the condition that gets checked in your conditional statement. The condition is set inside the parenthesis after if.
6. Set your condition so that if key
pressed is 'ArrowUp', the amount of pixelation in your video will change. For example:
if(key ==
"ArrowUp
"){
//increase pixelation on my video
}
In English, this is saying check if the key that’s pressed is ArrowUp, or the up arrow.
Have your students set their condition to key == "ArrowUp". Nothing should change yet. That's okay!
7. Now that a condition is set, your students will have to specify what happens when the up arrow is pressed. Remember, when the up arrow is pressed, the amount of pixelation affecting the video should increase.
Let's make this video get more pixelated! Increment means "add one to". It looks like this: pix.amount +=1;
Have your students add the code pix.amount +=1; inside of their conditional. The final code should look like this:
movie.whenKeyDown = function(key) {
if (key === 'ArrowUp'){
pix.amount +=1;
}
};
Try pressing the up arrow! Students’ videos should get more pixelated every time the up arrow is pressed.
8. Student Challenge: Write another conditional inside the whenKeyDown function that decreases pixelation when the down-arrow is pressed. Key should be set to 'ArrowDown'. You can always look at the final project if you need a hint!
Amazing! Now that the coding part of the project is done, your students can publish their projects. They can always go back later to add additional effects.
Part 3: Connecting to Makey Makey
Time to connect your Makey Makey! First, turn your Makey Makey on by plugging it into your computer with a USB cable.
You’ll want to connect to your up arrow to the Makey Makey. Connect an alligator clip to the up arrow on your Makey Makey. You’ll also connect an alligator clip to “Earth,” at the bottom. If you touch the two clips together, they’ll make a circuit, and your video will increase in pixelation!