psychopy.org | Reference | Downloads | Github

Reaction Time without Keyboard Component


#1

Hi all,

I am currently working on a memory experiment. I’ve been trying to use PsychoPy 3.0 builder to create it.

The participants will be asked to type the words that they encoded earlier in the experiment. I found a way to record this information on an excel file, but I also want to record the response time. I am NOT using a keyboard component for the recall portion which is why I’m having such a hard time. How do I record response times without using a keyboard component?

Thanks in advance.


#2

Show us how you are currently doing it.


#3

#Begin Experiment
import string
allLetters = list(string.ascii_lowercase)

#Begin Routine
if trials.thisN == 0: # only on the first trial
startTime = globalClock.getTime()
textFill = ‘’
storedAnswer_us.alignVert =‘top’
storedAnswer_us.alignHoriz =‘left’

#Each Frame
keys = event.getKeys(keyList=None, timeStamped=True)
if globalClock.getTime() - startTime >= 15:
trials.finished = True
continueRoutine = False
else:
if keys:
if keys[0] == ‘space’:
textFill += ’ ’ # Adds a space instead of ‘space’
elif keys[0] == ‘backspace’:
textFill = textFill[:-1] # Deletes
elif keys[0] in allLetters:
textFill+=keys[0] # Adds character to text if in alphabet.
screenText_us.setText(textFill) # Set new text on screen

    if keys[0] == 'return':                               
        storedAnswer_us.setText(screenText_us.text
                           + '\n' 
                           + storedAnswer_us.text)
        continueRoutine = False 

trials.addData(‘screenText_us.text’, screenText_us.text)

I added that code into the Routine, then I inserted a loop into the Routine that it’s pulling from an excel sheet. There are three different texts present in the routine which will allow participants to review the word that they’re typing, then they press ‘enter’ to add the word to a list on the top, left corner. The last text simply says, ‘List as Many U.S. Cities as Possible in Five Minutes’ (I put a 15-second time limit right now because I’m testing it) which is placed above the word list.

Anyhow, I simply need the reaction time for the participant to add the word to the list. So I only need a record of when they pressed ‘enter’ during the Routine. How do I do that?

Thanks, Michael!


#4

Could you please edit your post as below, so we can be sure that we are seeing your code with correct indenting?


#5
#Begin Experiment
import string
allLetters = list(string.ascii_lowercase)

#Begin Routine
if trials_3.thisN == 0: # only on the first trial
    startTime = globalClock.getTime()

textFill = ''
storedAnswer_us.alignVert ='top'
storedAnswer_us.alignHoriz  ='left'

#Each Frame
keys = event.getKeys()
for key in keys:
    if 'ctrl' and 'alt' and 'shift' in key:
        core.quit()

if globalClock.getTime() - startTime >= 15:
    trials_3.finished = True
    continueRoutine = False

else:
    if keys:
        if keys[0] == 'space':
            textFill += ' '  # Adds a space instead of 'space'
        elif keys[0] == 'backspace':
            textFill = textFill[:-1]  # Deletes
        elif keys[0] in allLetters:
            textFill+=keys[0]  # Adds character to text if in alphabet.
        screenText_us.setText(textFill)  # Set new text on screen
        
        if keys[0] == 'return':                               
            storedAnswer_us.setText(screenText_us.text
                               + '\n' 
                               + storedAnswer_us.text)
            continueRoutine = False 
trials.addData(‘screenText_us.text’, screenText_us.text)

#6

OK. Are you wanting to capture the reaction time to just the first key press, or the time associated with every keypress?


#7

I only need the response time for pressing ‘enter’ by adding the word to the list for each word. :slight_smile:


#8
if keys[0] == 'return': 
    thisExp.addData('word_complete_time', t)
    # t is the time elapsed since the start of the routine 
    # continue with your existing code here

Also note that this code will never cause a quit:

for key in keys:
    if 'ctrl' and 'alt' and 'shift' in key:
        core.quit()
  • you are examining key, which is a single item from a possible list of keys, so it can never contain more than one value.
  • So even if we change to checking for existence in the list of keys, in Python, checks like this have to be expressed individually, eg
if 'ctrl' in keys and 'alt' in keys and 'shift' in keys:
  • but lastly, in practice, this check would often fail even if you push all three keys. We run this check typically, say 60 times per second. The average person would press three keys like this in sequence rather than simultaneously, perhaps over 100s of milliseconds. So you are unlikely to find them all in the detected list of keys on a single call to getKeys(). That function empties the buffer of preceding keys each time it is called, so you are more likely to detect these keys individually or maybe one followed by a pair on the next check. It would be unlikely that they would all be pressed initially at exactly the same time.

#9

So how do I customize keys to quit the program while it’s running?


#10

You can easily use any individual key.

But if you want to specify a sequence of keys, then you need more verbose code, to cater for the fact that the sequence will likely take several iterations to be collected. ie on each check, record if any of the target keys have been detected. Only if and when all three have been pressed, then you quit. But you’d probably also need some temporal element so that detected keys expire after some time (ie they should all be detected within some short window of time).


#11

Thanks for your help before.

I ran into one last problem. I told you that I wanted the timing for the words which worked (thank you) but now I’m trying to use an if function so that participants cannot type in a word if a presented word is black. I tried the following code but an error came as “uncallable”.

#Each Frame
keys = event.getKeys()
if globalClock.getTime() - startTime >= 420:
    trials.finished = True
    continueRoutine = False

else:
    if keys:
        if presentedText == color(1.0): 
            if keys [0] == 'return':
                trials.finished = False
                continueRoutine = False
            else: 
                trials.finished = False
                continueRoutine = False
        if keys[0] == 'space':
            textFill += ' '  # Adds a space instead of 'space'
        elif keys[0] == 'backspace':
            textFill = textFill[:-1]  # Deletes
        elif keys[0] in allLetters:
            textFill+=keys[0]  # Adds character to text if in alphabet.
        screenText.setText(textFill)  # Set new text on screen
        if keys[0] == 'return':
            correctAnswer.setText(correctAnswer.text 
                                + '\n'
                                + presentedText.text)
                                
            storedAnswer.setText(storedAnswer.text 
                               + '\n' 
                               + screenText.text)
            textFill = ''
            screenText.setText(textFill)
            continueRoutine = False 
        if keys[0] == 'return':
            thisExp.addData('word_complete_time_recall', t)
trials.addData('screenText.text', screenText.text)

#12

I suspect the problem is here:

if presentedText == color(1.0):

I’m not familiar with the color() function. But regardless, it seems like you are trying to compare a text object to a colour, and this will never be true. You’d need to specifically compare the colour attribute of the text stimulus, something like this:

if presentedText.colour == 'black':