# Color polygon only when key is pressed

**URL:** <https://discourse.psychopy.org/t/color-polygon-only-when-key-is-pressed/26699>\
**Category:** Builder\
**Created:** [January 6, 2022, 8:30am UTC](https://discourse.psychopy.org/t/color-polygon-only-when-key-is-pressed/26699 "2022-01-06T08:30:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maya\_Kedem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/maya_kedem/32/31791_2.png) [@Maya\_Kedem](https://discourse.psychopy.org/u/Maya_Kedem)\
**Post date:** [January 6, 2022, 8:30am UTC](https://discourse.psychopy.org/t/color-polygon-only-when-key-is-pressed/26699/1 "2022-01-06T08:30:30Z")

</div>

Hi, I try to color a polygon only when a key (for example, ‘up’) is pressed. I tried several things, including adding the following code:

```auto
keys = event.getKeys(keyList=['up','down'])
for key in keys:
    if key == 'up':
        up_triangle.fillColor = [1,-1,-1] # red
    elif key == 'down':
       down_triangle.fillColor = [1,-1,-1] # red
    up_triangle.fillColor = [1,1,1] # back to white
    down_triangle.fillColor = [1,1,1] # back to white

```

but the back to the white line color at the end causes the polygon to not color. Thank you

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [January 6, 2022, 8:33am UTC](https://discourse.psychopy.org/t/color-polygon-only-when-key-is-pressed/26699/2 "2022-01-06T08:33:41Z")

</div>

You are immediately resetting the colour back to white. When do you want that to happen? For example

```auto
keys = event.getKeys(keyList=['up','down'])
for key in keys:
    if key == 'up':
        up_triangle.fillColor = [1,-1,-1] # red
        down_triangle.fillColor = [1,1,1] # back to white
    elif key == 'down':
       down_triangle.fillColor = [1,-1,-1] # red
       up_triangle.fillColor = [1,1,1] # back to white
    

```

---

<div class="post-metadata">

**Author:** ![Maya\_Kedem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/maya_kedem/32/31791_2.png) [@Maya\_Kedem](https://discourse.psychopy.org/u/Maya_Kedem)\
**Post date:** [January 6, 2022, 8:55am UTC](https://discourse.psychopy.org/t/color-polygon-only-when-key-is-pressed/26699/3 "2022-01-06T08:55:13Z")

</div>

Thanks, it’s better already  
I want that at the moment of pressing the key will be red, and at the moment of release it will be white again. That is, if someone presses an up arrow twice, it will turn red, return to white, and turn red again.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [January 6, 2022, 8:58am UTC](https://discourse.psychopy.org/t/color-polygon-only-when-key-is-pressed/26699/4 "2022-01-06T08:58:39Z")

</div>

event.getKeys can’t tell when a key is released. This is possible, but the coding is a bit more involved (so I often use mouse buttons when I want to monitor for the release as well). Alternatively, the triangle could change colour for a fixed length of time.

Becca’s example here might help.

> [@Recording KeyRelease data - iohub or keyBoard?](https://discourse.psychopy.org/t/recording-keyrelease-data-iohub-or-keyboard/26257/2):
>
> Hi There, Just to check that this is a study that will be run locally ? If so I think this demo should help [fetch duration.psyexp](https://discourse.psychopy.org/uploads/short-url/jCvaUqkDKlVqzEClYAGCfcXGu6Q.psyexp) (13.8 KB) Participants must hold down 2 keys for the trial to stat (the “1” and “2” keys) then they lift one and we show them which they lifted first. Hope this helps, Becca

---

<div class="post-metadata">

**Author:** ![Maya\_Kedem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/maya_kedem/32/31791_2.png) [@Maya\_Kedem](https://discourse.psychopy.org/u/Maya_Kedem)\
**Post date:** [January 6, 2022, 9:03am UTC](https://discourse.psychopy.org/t/color-polygon-only-when-key-is-pressed/26699/5 "2022-01-06T09:03:15Z")

</div>

Got it, thanks a lot
