You are on page 1of 3

Handling Key Press Events in Alice

One of the events that an Alice world can handle is When a key is typed. This event triggers
when the user presses a key and then releases it (i.e., the actual event occurs when the key is
released, not when the key is pressed). To create a tile for this event, click on create new event in the
Events Editor and select
When a key is typed from the drop-
down menu.

When you click on the any key placeholder, a menu will appear showing all of the keys that you can
select for this event. Available keys are <space>, <enter>, the arrow keys, letter keys, or number
keys.

When you click on the Nothing placeholder, a menu will appear showing all of the methods available
for this event. The available methods will depend on the objects you have included in your world, as
well as any custom methods you may have written.

Tutorial – Handling Key Press Events

1. Copy the world Faeries.a2w from the Handout folder to your workspace and then open your
copy of the world.

2. First you will create a fly method for the gossamerFlameGlimmer object (on the right). Select the
object and click on the create new method button. Name the new method fly.

3. In the gossamerFlameGlimmer.fly method, create the instructions as shown.

Page 1 of 3
Handling Key Press Events in Alice

4. We want the gossamerFlameGlimmer.fly method to execute when the user presses (and then
releases) the 'G' key on the keyboard. Create a new event for When a key is typed.

5. To complete the new event, you must assign any key to the letter 'G' and replace Nothing with the
method you created called gossamerFlameGlimmer.fly.

Page 2 of 3
Handling Key Press Events in Alice

6. Save the world and then play it. Any time you press the 'G' key, you should see the
gossamerFlameGlimmer object flap her wings and briefly fly.

7. Create an event tile that executes the mabHazelnut.fly method when the 'M' key is typed.

8. Save the world and then play it.

Page 3 of 3

You might also like