Interaction with the mouse and keyboard in Processing Python Mode

Environment:
the examples in this lesson are written for Processing Python Mode. If you work in the recommended environment Thonny + py5, most of the code stays the same, and names with a capital letter in the middle are written in lower case with an underscore: mouseX → mouse_x, noStroke() → no_stroke(). See the table of differences.

Lesson 7 of 20 · Python and Processing

Why is interaction important?


So far we have drawn and animated objects. Now we learn how to react to:

  • the position of the mouse
  • a mouse click
  • pressing a key

This is the basis for making games and interactive projects.

Interaction = the user affects the program in real time.

The position of the mouse — mouseX and mouseY



def setup():
    size(600, 400)

def draw():
    background(240)
    ellipse(mouseX, mouseY, 40, 40)
Result
The circle follows the mouse cursor in real time.

mouseX and mouseY are built-in variables that track the position of the cursor.

Tip: Add more circles of different sizes for a "mouse trail".

Detecting a click — mousePressed



def setup():
    size(600, 400)

def draw():
    background(240)

    if mousePressed:
        fill(255, 0, 0)
    else:
        fill(0, 150, 255)

    ellipse(300, 200, 120, 120)
Result
A mouse click changes the colour of the circle.

mousePressed is a logical variable (True/False).

This checks whether the button is being held down right now (not just a click).

The mousePressed() function


We can define a special function that is called when the user clicks:


def mousePressed():
    print("Click at position:", mouseX, mouseY)
The difference:

  • mousePressed → a state (whether the button is pressed)
  • mousePressed() → an event (a click has happened)

Example: a click adds a circle



circles = []

def setup():
    size(600, 400)

def draw():
    background(240)

    for c in circles:
        ellipse(c[0], c[1], 30, 30)

def mousePressed():
    circles.append((mouseX, mouseY))
Result
Every click adds a new circle to the screen.

Interaction with the keyboard — key and keyPressed()



x = 300

def setup():
    size(600, 400)

def draw():
    global x
    background(240)
    ellipse(x, 200, 50, 50)

def keyPressed():
    global x
    if key == 'a':
        x -= 10
    if key == 'd':
        x += 10
Result
The object moves left and right when you press the A and D keys.

We use:

  • key — which key was pressed
  • keyPressed() — the function that is triggered
You can also use the arrow keys (advanced): keyCode

Advanced: holding a key down



def draw():
    global x
    background(240)

    if keyPressed:
        if key == 'a':
            x -= 3
        if key == 'd':
            x += 3

    ellipse(x, 200, 50, 50)

Here the object moves while we hold the key down.


Mini project: an interactive scene


  • a ball follows the mouse
  • a click changes the colour
  • the keys move another object
Result
An interactive scene with several objects and controls.
Tip: Every object can have its own behaviour and controls.

Mini project (more advanced): a click changes the direction



dx = 2

def mousePressed():
    global dx
    dx *= -1  # changes the direction of movement

A click can change how an object behaves — this is the basis for games.


Comparison with the Java version


The same principles apply in Java Processing:

Processing with Java — interaction


Next lesson


Simple physics: gravity and bouncing →


Related topics




Check your knowledge

Open the quiz

Mini quiz: the mouse and the keyboard

1. Which variable gives the x position of the mouse in Python Mode?




2. When is the function mousePressed() called?