Julie Jules
Julie Jules

Reputation: 11

Psychopy: How do I get the participant to respond using a specific key on the keyboard. Blue=f?

Currently I have the code:

import random
from psychopy import visual, event
win = visual.Window()

# A TextStim and five of each word-color pairs
stim = visual.TextStim(win)
pairs = 5 * [('blue', 'blue'), ('yellow', 'blue'), ('green', 'yellow'), ('red','red')]
random.shuffle(pairs)

# Loop through these pairs
for pair in pairs:
    # Set text and color
    stim.text = pair[0]
    stim.color = pair[1]

    # Show it and wait for answer
    stim.draw()
    win.flip()
    event.waitKeys()

I am trying to assign the key 'f' on the keyboard to the colour red, 'g' to blue, 'h' to yellow and 'j' to green, so when the participant presses 'f' when they see the colour red, I want to record the reaction time and I want it to say 'correct' or 'incorrect'? I have tried so many codes but I think I am putting them in the wrong order or I am going somewhere wrong!

Upvotes: 1

Views: 466

Answers (1)

Jonas Lindeløv
Jonas Lindeløv

Reputation: 5683

A neat way to do this is to use python dictionaries as key-meaning mapping. So in the beginning, define

answer_keys = {'f': 'red', 'g': 'blue', 'h': 'yellow', 'j': 'green'}

As a simple demo, then

answer_keys['f'] == 'blue'  # False
answer_keys['g'] == 'blue'  # True

To use this in your experiment, do:

# Get answer and score it, and get RT
key, rt = event.waitKeys(keyList=answer_keys.keys(), timeStamped=True)[0]  # Only listen for allowed keys. [0] to get just first and only response
score = answer_keys[key] == pair[1]  # Whether the "meaining" of the key is identical to the ink color of the word.

# Show feedback
stim.text = 'correct' if score else 'incorrect'  # show text depending on score
stim.text += '\nRT=%i ms' %(rt*1000)  # Add RT to text, just for demo
stim.draw()
win.flip()
event.waitKeys()  # Wait, just to leave it on the screen for a while.

Upvotes: 1

Related Questions