Color polygon only when key is pressed

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:

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

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

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
    
1 Like

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.

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.

Got it, thanks a lot