| Reference | Downloads | Github

Play sound at key press



Hey all
I’m quite new to Psychopy and trying to do the following in my experiment:

It’s a dot motion task with 2 enabled response keys (left and right) where I want
- sound A played when the LEFT key is pressed
- and a randomly chosen sound A, B, C or D played when the RIGHT key is pressed

(the sounds are unrelated to whether the key response is correct)

Currently, I’ve implemented this in Psychopy builder with a code component which looks like this:

  • at the begin of my routine :

a_sound = sound.Sound(u’A’, secs=0.5)
b_sound = sound.Sound(u’B’, secs=0.5)
c_sound = sound.Sound(u’C’, secs=0.5)
d_sound = sound.Sound(u’D’, secs=0.5)

  • and at each frame I’ve got this:

response = event.getKeys(keyList=[‘left’,‘right’])

if ‘left’ in response:
elif ‘right’ in response:

Two problems emerge when I try to run this:

  • the left key sound only occurs after a double left key press instead of just one
  • the randomiser function does not seem to work at all (experiment shuts down after right key press)

Any suggestions of what I’m doing wrong / or how to implement the same thing in another way? Really any help would be much appreciated!


Not an expert. Just wondering, if you import numpy as np at your begin routine?


If that indeed was the problem, you should also use ‘np.random.choice(RandList)’, where ‘RandList’ is a list of sounds that you want to be played randomly.