Keyboard responses in builders interfering with code?

OS (e.g. Win10): win10
PsychoPy version (e.g. 1.84.x): pyschopy 2022.2.5
Standard Standalone? (y/n) If not then what?: standard standalone

Hi all, I have
What are you trying to achieve?:
I’m new to pyschopy and coding and currently designing an experiment on time perception. For my experiments participants have to estimate time duration of a stimulus by pressing and holding the space bar, releasing the space bar once the estimated time duration ends. I have created a experiment in builder, using some code components. However the problem I’m having is that in my data sheet (in excel) the estimate duration column (‘’Hold times’’) produced two estimates. I need the only one estimate value to appears??
This is the code I’m using

Begin routine

duration = []
rt = []
trialClock = core.Clock()  # if this doesn't work for some reason use core.Clock() instead
kb = keyboard.Keyboard(clock=trialClock)

Each frame

keys2 = kb.getKeys()
for key in keys2:
    if key.duration:
    if key.rt:

End routine

Practice_Trials_Loop.addData("Hold Times", duration)
Practice_Trials_Loop.addData("Response Time", rt)

What did you try to make it work?:
I have some keyboard responses throughout my experiment and my guess is that these are somehow affecting the estimation duration. Most of these are pressing the ‘spacebar’ to advance in the experiment. With some feedback responses by pressing to alphabets eg. Either ‘p’ or ‘n’.
I wanted to know if it was possible to maybe use code to get these response instead of using the keyboard response. So that it doesn’t mess with the ‘spacebar hold time’.
Can anyone help ??

What specifically went wrong when you tried that?:

Hi everyone

Any help would be sincerely appreciated