HomeTurtlegraficsGPanelRobotics WebTigerPython |
Python - Online |
Deutsch English |
YOU LEARN HERE... |
how you can influence the program flow by pressing keyboard keys. Similar to the mouse events, you define in a callback function what should happen when a keyboard key is pressed. The callback function is not called by the program, but automatically by the system when a key is pressed. |
EXAMPLES |
Example 1: The Turtle draws a kick when any key is pressed
Program:
|
Program: from gturtle import * def onKeyPressed(key): if key == "ArrowLeft": setHeading(-90) elif key == "ArrowRight": setHeading(90) elif key == "ArrowUp": setHeading(0) elif key == "ArrowDown": setHeading(180) forward(10) makeTurtle(keyPressed = onKeyPressed) speed(-1) addStatusBar(20) setStatusText("Use cursor keys to move me!") |
REMEMBER... |
is not called by your program, but by the system when you have pressed a key. You can display the name of the keys, in particular the special keys, in the output window with print(key). The callback function is registered as a parameter of makeTurtle(). |
TO SOLVE BY YOURSELF |
|