Drawing while button is pressed

Hello! We are trying to have a stimulus play/update/draw while a button is pressed and stop drawing immediately when the button is released. We also need to record the duration that the button was pressed.

Our first idea was to use kb.waitKeys(waitRelease = True), however waitKeys pauses all drawing until the key is released so our stimulus can not play/update/draw.

Our second idea was to just use event.waitKeys() without a waitRelease and start a while loop that plays a video after the button is pressed and have the while loop end when the button is released. Does anyone know how to code a button being released?

Here’s our code for the second idea:

from psychopy import visual, core, event
from psychopy.hardware import keyboard

def image_writer(win_in, image_in, x_size_in, y_size_in):
    '''Function that puts an image on a window by taking in a window,
        image, and x and y size scalars. Outputs a draw (NOT a flip)'''
    img = visual.ImageStim(win=win_in, image=image_in, units="pix")
    size_x = img.size[0]
    size_y = img.size[1]
    img.size = [size_x * x_size_in, size_y * y_size_in]
    img.draw()

### Experiment----------
'''Create the window'''
win = visual.Window([800,800], monitor = 'testMonitor', color = 'black')
'''Start Clock'''
clock = core.MonotonicClock()
'''Set Keyboard'''
kb = keyboard.Keyboard()

'''Start Experiment'''
image_writer(win, 'SoundStart.jpg', 1, 1) # show the starting image
time_soundbar = clock.getTime() # get the time
win.flip()
event.waitKeys(keyList = ['0']) # wait for the press to begin

mov = visual.MovieStim3(win, 'SNAT_soundbar.mp4', noAudio=True, units = 'pix', size = (1000, 1000)) # video to play
core.wait(.1)
timer = core.CountdownTimer(mov.duration) # start a timer for while loop
while timer.getTime() > 0: # This is the line we need help with!
    mov.draw()
    win.flip()

current_pic = "Sound1.jpg" # Filler code at the moment
#^ We need duration of button press for that line
image_writer(win, current_pic, 1, 1)
win.flip()
core.wait(2)