# Need quicker response to key press

**URL:** <https://discourse.processing.org/t/need-quicker-response-to-key-press/37298>\
**Category:** Processing.py\
**Created:** [June 3, 2022, 9:55am UTC](https://discourse.processing.org/t/need-quicker-response-to-key-press/37298 "2022-06-03T09:55:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonc8](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@simonc8](https://discourse.processing.org/u/simonc8)\
**Post date:** [June 3, 2022, 9:55am UTC](https://discourse.processing.org/t/need-quicker-response-to-key-press/37298/1 "2022-06-03T09:55:33Z")

</div>

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](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

```auto
#!/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()

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 3, 2022, 10:26am UTC](https://discourse.processing.org/t/need-quicker-response-to-key-press/37298/2 "2022-06-03T10:26:54Z")

</div>

> [@simonc8](#):
>
> Is there a way to eliminate this delay?

I believe you need both **[keyPressed()](https://py.processing.org/reference/keyPressed.html)** & **[keyReleased()](https://py.processing.org/reference/keyReleased.html)** for a smooth movement:

https://trinket.io/embed/python/62fab051ed?toggleCode=true&runOption=run

---

<div class="post-metadata">

**Author:** ![simonc8](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@simonc8](https://discourse.processing.org/u/simonc8)\
**Post date:** [June 3, 2022, 10:57am UTC](https://discourse.processing.org/t/need-quicker-response-to-key-press/37298/3 "2022-06-03T10:57:24Z")

</div>

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!
