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:

https://trinket.io/library/trinkets/29f9ec9e9a

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

#!/bin/python3

from processing import *

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

def setup():
  size(400,400)

def draw():
  global x, y
  background(200)
  fill(fillvalue)
  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


run()
1 Like

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

2 Likes

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!

2 Likes