Skip to content

keyIsPressed()

A boolean variable that's true if any key is currently pressed and false if not.

Examples

keyIsPressed example 1

function setup()
  size(100, 100)

  describe(
    'A gray square with a white square at its center. The white square turns black when the user presses a key.'
  )
end

function draw()
  background(200)

  -- Style the square.
  if keyIsPressed == true then
    fill(0)
  else 
    fill(255)
  end

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

keyIsPressed example 2

function setup()
  size(100, 100)

  describe(
    'A gray square with a white square at its center. The white square turns black when the user presses a key.'
  )
end

function draw()
  background(200)

  -- Style the square.
  if keyIsPressed then
    fill(0)
  else 
    fill(255)
  end

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

keyIsPressed example 3

function setup()
  size(100, 100)

  describe(
    'A gray square with the word "false" at its center. The word switches to "true" when the user presses a key.'
  )
end

function draw()
  background(200)
  fill(0)

  -- Style the text.
  textAlign(CENTER)
  textSize(16)

  -- Display the value of keyIsPressed.
  text(keyIsPressed, 50, 50)
end

Syntax

keyIsPressed