Need quicker response to key press

I’m using processing in a Python trinket for our Code Club to move a shape around the window using the arrow keys. But there’s a significant delay after pressing the key before the shape moves, which makes this almost useless for coding quick reaction games. Is there a way to eliminate this delay?

Here is the code:

Warning: this link now points to the code after it was fixed, so it won’t make sense to anyone trying to work out what my problem was… Here is the code before it was fixed


from processing import *

# Click on the image to give it focus,
# and then press any key.
fillvalue = 0
x = 25
y = 25

def setup():

def draw():
  global x, y
  rect(x, y, 50, 50)

def keyPressed():
  global value, x, y
  if keyCode  == RIGHT:
    x += 1
  if keyCode  == LEFT:
    x -= 1
  if keyCode  == UP:
    y -= 1
  if keyCode  == DOWN:
    y += 1

1 Like

I believe you need both keyPressed() & keyReleased() for a smooth movement:


Thanks. I knew there was a way. I shall adapt your code to simplify it for our code club members, avoiding OOP which we haven’t progressed to yet!