P5 Python Docs
  • Getting Started
  • Simple mode
  • Reference
    • Shapes
      • 2D
        • point()
        • line()
        • rect()
        • square()
        • triangle()
        • circle()
        • ellipse()
        • arc()
        • quad()
        • beginShape()
      • 3D
        • plane()
        • box()
        • sphere()
        • cone()
        • cylinder()
        • ellipsoid()
        • torus()
    • Controlling the environment
      • coordinateMode()
      • rectMode()
      • ellipseMode()
      • frameRate()
      • setup()
      • draw()
    • Built-in Variables
      • Environment
      • Keyboard
      • Mouse
    • Built-in Functions
      • keyIsDown()
      • millis()
      • second()
      • minute()
      • hour()
      • translate()
      • rotate()
      • createSlider()
      • createGraphics()
      • createVector()
    • Events
      • Keyboard
        • keyPressed()
        • keyReleased()
      • Mouse
        • mouseMoved()
        • mouseDragged()
        • mousePressed()
        • doubleClicked()
        • mouseWheel()
    • Text
      • text()
      • textSize()
      • textAlign()
    • Math
      • drawTickAxes()
      • random()
      • randomGaussian()
      • dist()
      • linmap()
      • bounce()
      • wave()
    • Colour and Outline
      • colorMode()
      • fill()
      • stroke()
      • strokeWeight()
      • background()
    • Images & Audio
      • loadImage() and image()
      • loadSound()
    • 3D Controls
      • orbitControl()
      • translate()
      • rotateZ()
      • rotateX()
      • rotateY()
      • scale()
Powered by GitBook
On this page
  • Example
  • Default inputs
  • Description
  1. Reference
  2. Built-in Functions

keyIsDown()

Checks whether a certain key is down

PreviousBuilt-in FunctionsNextmillis()

Last updated 2 years ago

Example

# W keyCode = 87
if keyIsDown(87):
    # move square up
    # ...
    
# S keyCode = 83
if keyIsDown(83):
    # move square down
    # ...
    
# A keyCode = 65
if keyIsDown(83):
    # move square left
    # ...
    
# D keyCode = 68
if keyIsDown(83):
    # move square right
    # ...
Example Full Code
def setup():
  createCanvas(400,400)
  textAlign(CENTER, CENTER)
  textSize(20)
  

squareX = 175
squareY = 100
squareSize = 50

def draw():
  global squareX, squareY, squareSize
  background("hotpink")

  fill(255, 255, 28)
  noStroke()

  if keyIsDown(87):
    squareY += 1
    text('"W" is Down(Pressed) now',200,350)
    
  if keyIsDown(83):
    squareY -= 1
    text('"S" is Down(Pressed) now',200,350)

  if keyIsDown(65):
    squareX -= 1
    text('"A" is Down(Pressed) now',200,350)

  if keyIsDown(68):
    squareX += 1
    text('"D" is Down(Pressed) now',200,350) 

  stroke('black')
  fill (112,88,255) 
  square(squareX,squareY,squareSize)

Default inputs

keyIsDown(VALUE)
Input
Description

VALUE

key code of the key being checked

Description

The keyIsDown() function checks if a certain key is currently down (Pressed). It returns True if that key is down, and False if it does not. (Boolean datatype)

For example, keyIsDown(38)checks whether the up arrow is being pushed, since 38 is its keycode.

Similarly, keyIsDown(UP_ARROW)checks for the same thing, because UP_ARROW is a constant that stores the key code of the up arrow, other such constants include DOWN_ARROW, ENTER and SHIFT.

In general, you can search the web for: key code of ### or look at .

Find these documents helpful? Let the people who made them help your child learn to code at !

We'd love to hear your Feedback/Comments .

this website
Strivemath
here