Skip to content

Touchscreen Drawing

In this short tutorial, you’ll learn how to use the touchscreen to interact with your program.

We’ll make a simple drawing app:

  • Drag your finger to draw lines
  • Press [EXE] to clear
  • Press [CLEAR] to exit

ClassPad touch input is handled through the event system, just like keys.
You can listen to:

  • KEYEV_TOUCH_DOWN: when your finger touches the screen
  • KEYEV_TOUCH_DRAG: while you’re dragging
  • KEYEV_TOUCH_UP: when you lift your finger

Each event has .x and .y fields for the position (in pixels).


We create a function clear() that:

  • Clears the screen
  • Draws a helpful label at the top
def clear():
dclear(C_WHITE)
dtext(2, 2, C_BLACK, "Touch: draw | EXE: clear | Clear: quit")

We then call it once at the beginning:

clear()
dupdate()

We use two variables to remember the last touch location:

x, y = -1, -1

Every time the screen is touched, we save where it started.


We use pollevent() to listen to input:

ev = pollevent()
while ev.type != KEYEV_NONE:
# Handle events...
ev = pollevent()

This loop catches every type of input — key presses and touches.


if ev.type == KEYEV_TOUCH_DOWN:
x, y = ev.x, ev.y

This saves the starting point when you first touch the screen.

if ev.type == KEYEV_TOUCH_DRAG:
dline(x, y, ev.x, ev.y, C_BLACK)
x, y = ev.x, ev.y

This draws a line between the last known point and the new one, creating a fluid pen stroke.


if ev.type == KEYEV_DOWN and ev.key == KEY_EXE:
clear()

Press [EXE] to clear the screen and restart drawing.

if ev.type == KEYEV_DOWN and ev.key == KEY_EXIT:
break

You can optionally add this to allow quitting with [CLEAR].


from gint import *
def clear():
dclear(C_WHITE)
dtext(2, 2, C_BLACK, "Touch: draw | EXE: clear | Clear: quit")
clear()
dupdate()
x, y = -1, -1
while True:
ev = pollevent()
while ev.type != KEYEV_NONE:
if ev.type == KEYEV_DOWN and ev.key == KEY_EXE:
clear()
if ev.type == KEYEV_TOUCH_DOWN:
x, y = ev.x, ev.y
if ev.type == KEYEV_TOUCH_DRAG:
dline(x, y, ev.x, ev.y, C_BLACK)
x, y = ev.x, ev.y
ev = pollevent()
dupdate()

Want to try more touch projects? Try:

  • A touch paint app with colors
  • A grid tile editor
  • A basic drag & drop UI

Need help with ideas? Ask the 👨‍💻 PythonExtra ChatGPT!