psychopy.org | Reference | Downloads | Github

Spacebar Release Duration Issue

Mac PsychoPy 2020 1.2

Hi there, I am very very new to PsychoPy so my apologies if this question is very basic (and or unclear :confused: )

  • What I am trying to do is create a single routine which presents two text stimuli to the participant.
    -I want the first stimuli to disappear and be replaced by the second stimuli when the participant presses and holds the spacebar.
    -I want the routine to end when participant presses either of the keys ‘y’ or ‘t’ and for the duration time of the spacebar hold to be recorded.

So far I have tried this using both the coder and builder views but with little success.

Are both text stimuli in the same location?

So the second stimulus should disappear when they release the space bar? Can they press y or t while still pressing space?

Sorry I was unclear it should be as follows:

-The participant will be instructed to only use one finger for the duration of the task.

  • The First Stimulus should Disappear after the the participant presses and holds the space bar for an arbitrary amount of time (lets say two seconds), after these two seconds the second stimulus should appear.

-When the second stimulus appears the participant will make a choice and move there finger from the spacebar to either ‘t’ or ‘y’.

-When they press either ‘t’ or ‘y’ the routine ends.

-Ideally, in a later iteration I will try to restart the routine if they spend to long in between releasing the spacebar and pressing ‘y’ or ‘t’, but if I get as far as ending the routine for now I would be happy.

You can do this in Builder but you won’t be able to use the graphical Keyboard component - you’ll need to do your own keypress monitoring in code via a code component’s “each frame” tab. That way you can call the .getKeys() function of the Keyboard class and use the default waitRelease = True behaviour. i.e. when you call that function, even if the space bar is pressed, it won’t give you a result until the space bar has been released, at which time you will get both the onset time of the keypress and its duration.

The Builder Keyboard component checks with waitRelease = False, so if you use that to end the routine, it will happen in response to the key being pressed, not to it being released.

https://www.psychopy.org/api/hardware/keyboard.html

1 Like

Hi Micheal,

Thanks for the suggestion, I tried adding in the keyboard all day to the builder with no luck.

Further, I tried using the .getkeys function you suggested but could not get my program to function using it. (Again, I am very new to PsychoPy and Python in general so its definitely a lack of understanding on my side.)

I was however able to code a bare bones version of what I want to do using the old event.waitKeys. I was wondering if its possible to record the key duration time ( or a good proxy measure) from what I am able to code.

The code is as follows:

#!/usr/bin/env python
# -*- coding: utf-8 -*-


from psychopy import visual, core, event



#Window
win = visual.Window([400,300], monitor="testMonitor")


a = ["1","2"]

#First Action
for k in a:
	i = 0
	c = None
	f = 0
	d = None
	while c==None:   # loop until a key is pressed <- would like to measure duration
		message = visual.TextStim(win, text=str(i)+"Press space bar when you are ready to answer")
		message.draw()
		win.flip()
		c = event.waitKeys(maxWait=1.0, keyList = ['space'])
		i = i + 1
	while d==None:   # loop until a key is pressed
		message = visual.TextStim(win, text=str(f)+"mover your finger to the y or t keys to choose")
		message.draw()
		win.flip()
		d = event.waitKeys(maxWait=1.0, keyList = ['y','t'])
		f = f + 1

You generally can’t use .waitkeys() in Builder, it completely disrupts its drawing cycle and timing by pausing indefinitely.

You also can’t use the event module to record the duration of a keypress. That is one of the advantages to switching to the newer keyboard class.

1 Like

Hi there,

As @Michael mentioned you can use the keyboard class to measure duration. Here is a post with a code snippet that should work in your coder view.

The focus of this post is on time locking the start of measurements but the code there is the same. One thing I would say is if you can try to implement this as code snippits in builder that would probably be simplest in the long run. But at the moment hopefully this code snippits helps in showing how you can implement the keyboard class!

Becca

1 Like

Dear Becca,

Thank you very much, The code had exactly what I need :slight_smile:

Sean

1 Like