Processing Mouse and Keyboard Input

First, know the processing automatically records your last mouse button in a special variable called mouseButton as well as the last key you pressed as keyCode.

These values are recorded as numbers. Example:
mouseButton values:
Left click == 37
Right click == 39

keyCode values
a-key == 65
b-key == 66
...
z-key == 90

Left-arrow key == LEFT
Right-arrow key == RIGHT


Each time you press a key, processing automatically calls a function named keyPressed.
Each time you release a key, processing automatically calls a function named keyReleased.

Example:

//This script will draw a circle on the screen if the a-key is being pressed
//and it will draw a rectangle on the screen if the b-key is being pressed
//it will not draw either shape if these keys are not being pressed

var a_pressed = false;
var b_pressed = false;

var draw = function() {
  background(200)
  if (a_pressed) {
    ellipse(250,250,100,100);
  }
  if (b_pressed) {
    rect(150,150,200,200);
  }
}

var keyPressed = function() {
  if (keyCode == 65) {
    a_pressed = true;
  }

  if (keyCode == 66) {
    b_pressed = true;
  }
}

var keyReleased = function() {
  if (keyCode == 65) {
    a_pressed = false;
  }

  if (keyCode == 66) {
    b_pressed = false;
  }
}

Processing Mouse and Keyboard Input courses

Bring Your Child to Work Day 2019 (Beginner)

Visual Effects lessons

VFX lessons in JavaScript

Visual Effects lessons

VFX lessons in JavaScript

Helicopter Game

Create a game where the player pilots a helicopter with the mouse and collects coins while avoiding obstacles

Visual Effects lessons

VFX lessons in JavaScript

Processing Mouse and Keyboard Input lessons

Code Clicker JavaScript

Create a basic code clicker game

Etch-A-Sketch

basic drawing with dots

MV Paint

Create a simple program to draw with the mouse

Helicopter Game: Movement and Coins

In the first part of this course we will be making a simplified version of the game which allows the player to move and to collect coins

ProcessingJS Coloring Book

Learn to use ProcessingJS functions

Related Links

No links found
About Processing Mouse and Keyboard Input

processing has built-in functions that activate when you press on the mouse or keyboard

Badge

Processing Mouse and Keyboard