SHOP

PLAY

CREATE

5

Movement (1): Keyboard INPUT

Min. Age: 4+ Mother and son; age icon Duration: 20 min. Mother and son; age icon

Video Example:

Material required: Download material .pdf

1

What's the goal?

Create a program that can read the keys you press on the keyboard. It will be useful for moving the Knight through the dungeon.

2

What should my program do?

Thanks to the keyboard assignment block your program can read the keys you press. Place the blocks as follows:

Block-based program with a keyboard input loop and keyboard input printing
3

How do I execute my program?

1

Start the program at the BEGIN block and drag the 'k' variable into the keyboard assignment block.

As an example, press the 'right arrow' key. This key has the number '6', so the value '6' is assigned to the 'k' variable:

Finger pressing the key 6 of a keyboard block

"Assigning the value '6' to the variable 'k' "

2
Keyboard input loop exit condition block

Drag the 'k' variable into the condition block:
-If 'k' is different from '27' continue in the loop as indicated by the direction arrows Code direction block ,
-Otherwise 'k' equals '27' and the program ends.

3

Once in the code block, call the 'print' function, passing the 'k' variable as an argument such as print( k ). Write the value of the 'k' variable on your whiteboard.

Function print call, printing k where k equals 6

"Calling the print function".

4

Once the value of 'k' has been "printed" thanks to the 'print' function, follow the direction arrows until entering the keyboard assignment block again. There, wait until the player presses a key to reassign a new value to the 'k' variable.

The loop breaks when the player presses the ESC ("Escape") key ESCAPE key with code 27 on it. (k=27), at which point the condition 'k != 27' is no longer met and the program exits.

Keyboard input loop