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: When the r key is pressed, a red circle is drawn, the b key a blue circle and the g key a green circle.
After starting the program, you must first click with the mouse in the graphics window, otherwise the keyboard clicks will be executed in the editor window. Program: #Gp12a.py from gpanel import * def onKeyPressed(key): print(key) if key == 'r' or key == 82: setColor("red") elif key == 'b' or key == 66: setColor("blue") elif key == 'g' or key == 71: setColor("lime") fillCircle(3) makeGPanel(-10, 10, -10, 10, keyPressed = onKeyPressed) print("Use key r, g, or b.") |
Since the value of the variables x, y is changed in a function, these variables must be defined as global. Program: #Gp12b.py from gpanel import * def onKeyPressed(key): global x, y if key == 'ArrowUp' or key == 38: y += 2 elif key == 'ArrowDown' or key == 40: y -= 2 elif key == 'ArrowLeft' or key == 37: x -= 2 elif key == 'ArrowRight' or key == 39: x += 2 makeGPanel(-100, 100, -100, 100, keyPressed = onKeyPressed) x = 0 y = 0 setColor("lime") lineWidth(5) print("Use cursor keys to move the cursor!") while True: draw(x, y)
In the callback function, you only define the state. The new coordinates are determined in the setMove() function. You can control the speed of the movement with delay(). Program: #Gp12c.py from gpanel import * def onKeyPressed(key): global state if key == 'ArrowUp' or key == 38: state = 'UP' elif key == 'ArrowDown' or key == 40: state = 'DOWN' elif key == 'ArrowLeft' or key == 37: state = 'LEFT' elif key == 'ArrowRight' or key == 39: state = 'RIGHT' def setMove(): global x, y if state == 'UP': y += 2 elif state == 'DOWN': y -= 2 elif state == 'LEFT': x -= 2 elif state == 'RIGHT': x += 2 makeGPanel(-100, 100, -100, 100, keyPressed = onKeyPressed) x = 0 y = 0 setColor("lime") lineWidth(5) state = '' while True: setMove() draw(x, y) delay(100)
Program: #Gp12d.py from gpanel import * def onKeyPressed(key): global state if key == 'Escape' or key == 27: state = 'stop' elif key == 'Enter' or key == 13: state = 'go' def setMove(): global x, dx if state == 'go': x = x + dx if x > 80 or x < -80: dx = -dx elif state == 'stop': return makeGPanel(-100, 100, -100, 100, keyPressed = onKeyPressed) x = 0 y = 0 dx = 1 state = '' while True: setMove() pos(x, y) setColor("red") fillCircle(5) delay(10) setColor("white") fillCircle(5.2) |
REMEMBER YOU... |
The callback function onKeyPressed(key 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 makeGPanel(). The callback function should not contain any longer program blocks. |
TO SOLVE BY YOURSELF |
|