Skip to content

keyReleased()

A function that's called once when any key is released.

Declaring the function keyReleased() sets a code block to run once automatically when the user releases any key:

function keyReleased() 
  -- Code to run.
end

The key variables will be updated with the most recently released value when keyReleased() is called by L5:

function keyReleased() 
  if key == 'c' then
    -- Code to run.
  end

  if key == 'return' then
    -- Code to run.
  end
end

Examples

keyReleased example 1

local value = 0

function setup()
  size(100, 100)

  describe(
    'A gray square with a black square at its center. The inner square changes color when the user releases a key.'
  )
end

function draw()
  background(200)

  -- Style the square.
  fill(value)

  -- Draw the square.
  square(25, 25, 50)
end

-- Toggle value when the user releases a key.
function keyReleased()
  if value == 0 then
    value = 255
  else 
    value = 0
  end
end

keyReleased example 2

local value = 0

function setup()
  size(100, 100)

  describe(
    'A gray square with a black square at its center. The inner square becomes white when the user releases the "w" key.'
  )
end

function draw()
  background(200)

  -- Style the square.
  fill(value)

  -- Draw the square.
  square(25, 25, 50)
end

-- Set value to 255 the user releases the 'w' key.
function keyReleased()
  if key == 'w' then
    value = 255
  end
end

keyRelease example 3

local value = 0

function setup()
  size(100, 100)

  describe(
    'A gray square with a black square at its center. The inner square turns white when the user presses and releases the left arrow key. It turns black when the user presses and releases the right arrow key.'
  )
end

function draw()
  background(200)

  -- Style the square.
  fill(value)

  -- Draw the square.
  square(25, 25, 50)
end

-- Toggle the background color when the user releases an arrow key.
function keyReleased()
  if key == 'left' then
    value = 255
  elseif key == 'right' then
    value = 0
  end
end

Syntax

keyReleased()