QUICK REFERENCE
Input
Respond to keyboard and pointer events using pyDraw's automatic handler registration.Registering input handlers
Define top-level functions with pyDraw's recognized names, then call listen() after those definitions. pyDraw finds the functions and connects them to the Screen.
screen.listen()Register the supported input functions currently defined by the program.
keydown(key) · keyup(key)Handle keyboard presses and releases.
mousedown(location, button) · mouseup(location, button)Handle pointer-button presses and releases.
mousedrag(location, button) · mousemove(location)Handle pointer movement with or without a held button.
def keydown(key):
print("pressed:", key)
def mousedown(location, button):
player.center(location)
screen.listen()
screen.loop()Keyboard input
Keyboard handlers receive a Screen.Key value normalized to lowercase. It compares directly with strings, including readable names for special keys.
keydown(key)Run when a key is pressed; holding a key may produce repeated presses.
keyup(key)Run when a pressed key is released.
key.key() · str(key)Return the normalized key name as a string.
def keydown(key):
if key == "left":
player.move(-5, 0)
elif key == "right":
player.move(5, 0)
elif key == "space":
player.move(0, -20)held = set()
def keydown(key):
held.add(str(key))
def keyup(key):
held.discard(str(key))Pointer buttons
Button handlers receive the event position as a Location and the pressed button as an integer. Use the location supplied to the callback for the exact event coordinates.
mousedown(location, button)Run when a pointer button is pressed.
mouseup(location, button)Run when that pointer button is released.
button == 1 · button == 2 · button == 3Identify the left, middle, and right mouse buttons.
def mousedown(location, button):
if button == 1 and target.contains(location):
target.color(Color("yellow"))
def mouseup(location, button):
if button == 1:
target.color(Color("blue"))Movement, dragging & pointer state
mousemove handles ordinary movement. Once a button is held, movement is delivered to mousedrag until the corresponding mouseup, including a drag that temporarily leaves the Screen.
mousemove(location)Run when the pointer moves over the Screen without dragging.
mousedrag(location, button)Run for movement while a pointer button is held.
screen.mouse()Return the latest tracked pointer position as a Location.
brush = Pen(screen, 0, 0, Color("blue"), width=5)
def mousedown(location, button):
if button == 1:
brush.moveto(location)
brush.start()
def mousedrag(location, button):
if button == 1:
brush.moveto(location)
def mouseup(location, button):
if button == 1:
brush.stop()def mousemove(location):
cursor.center(location)
# Elsewhere in the program:
latest = screen.mouse()Event dispatch & updates
Input is processed by the global Screen update cycle. Events are delivered in order, handlers run, and their drawing changes are presented by the same update.
screen.loop()Keep the program open and process input automatically.
screen.update()Process queued input and present drawing changes once inside a custom global loop.
screen.listen()
while True:
update_game()
screen.update()
screen.sleep(1 / 60)Console input
Python's built-in input() reads a line of text. It is separate from Screen keyboard handlers and returns the submitted value as a string.
input(prompt="")Display an optional prompt, wait for a line of text, and return it without the ending newline.
print(*values)Write values to the terminal or browser console.
name = input("What is your name? ")
print("Welcome,", name)
age = int(input("How old are you? "))