Getting keybaord press duration - And presenting a stimulus accordingly

Hello, further to this topic:

I would be happy for a little help.

With what code can I use so that the circles (polygons) will be added according to the duration of pressing the space bar on the keyboard until release

Say, one circle will be added every 500 milliseconds.

I saw and used this code as a starting point:

from psychopy.hardware import keyboard
from psychopy import core

kb = keyboard.Keyboard ()

kb.clock.reset () # when you want to start the timer from
keys = kb.getKeys ([‘right’, ‘left’, ‘quit’], waitRelease = True)
if ‘quit’ in keys:
core.quit ()
for key in keys:
print (key.name, key.rt, key.duration)

And I added

if key.duration> 0.5:
print (0)

Just to test the possibility that this code will work, but I get an error:

name ‘key is not defined’

thanks alot!

Is this indented to put it in the for key in keys loop?

i’m realy new with coding - so i dont sure what do you meant by ‘indented’

In Python the start and end of loops and conditionals are defined by indents. See this post for an example.

I JavaScript the indents are translated into {brackets}

1 Like

i think its ident

-------Run Routine "trial"-------
while continueRoutine:
    # get current time
    t = trialClock.getTime()
    tThisFlip = win.getFutureFlipTime(clock=trialClock)
    tThisFlipGlobal = win.getFutureFlipTime(clock=None)
    frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
    # update/draw components on each frame
    kb = keyboard.Keyboard()
    
    during your trial
    kb.clock.reset()  # when you want to start the timer from
    keys = kb.getKeys(['right', 'left', 'quit'], waitRelease=True)
    if 'quit' in keys:
        core.quit()
    for key in keys:
        print(key.name, key.rt, key.duration)
    
    if key.duration > 0.5:
        print(0)

Try

    for key in keys:
        print(key.name, key.rt, key.duration)  
        if key.duration > 0.5:
            print(0)

1 Like

ok it worked!

@wakecarter thank u!

Hi there,

Pleased to see you have a solution here. As something that might help here is a similar situation where we increase the size of a polygon based on key press duration Rebecca Hirst / OSARI_online · GitLab for you it should be slightly simpler because you can directly change the size parameter rather than specifically changing two vertices (this is a “filling bar”).

Hope this helps and pleased to see you are making progress,
Becca

1 Like