Move a stimulus by pressing a button

Hi everyone!
I am trying to make my stimulus move constantly from the left to the right and move up and down if certain buttons are pressed.
The position of the stimulus is set every frame by (pos_x, pos_y).

In the code component, I have
Begin experiment:

pos_x = -0.5 # the initial x coordinates
pos_y = 0 # the initial y coordinates
plus_y = 0.03
minus_y = -0.03

Each frame:

pos_x = pos_x + 0.001 # this makes the stimulus constantly move from the left to the right

if warm.keys == "up":
    pos_y = pos_y + plus_y  # if the UP button is pressed move the stimulus up
elif warm.keys == "down":
    pos_y = pos_y + minus_y # if the DOWN button is pressed move the stimulus down
    pos_y = pos_y

It all works perfectly except the stimulus doesn’t stop moving after pressing the UP or DOWN button. I’d like it to just bounce 0.03 units up or down if the button was pressed. Instead, pressing the button makes it constantly moving up or down until the opposite button is pressed (then it starts moving in the opposite direction).

I’m not good at coding and I think I miss something very simple. Would be happy to get some advice!

P.S. I also want my stimulus to leave a line behind it (some sort of a footprint). May be some of you know how to do it?

We don’t know what warm.keys is, but presumably it retains its value until it changes to something else. So when you move the stimulus up or down, you should also re-set warm.keys to some value other than 'up' or 'down' so it doesn’t keep triggering your code.