Adding sound on key press without ending the trial

I’m new to psychopy and trying to build an experiment. In this we want the participants to press a key when they see the target slide and when they press the key they should hear a sound in the form of feedback so that they know they pressed the key.
We want to add sound to pressing a specific key - without ending the trial participants should continue the experiment and be able to press that key whenever they see the targeted slide and should hear a sound on pressing the key in the form of feedback.

Is there any way of adding sound on key press without using code? can anyone help please?

Hello gull

well, that should be possible. For instance add

$'left' in key_resp.keys

to the Start-property of your sound-component and change it from time (n) to condition. Now your sound is played when you press the left cursor-key.

Best wishes Jens

Hello Jens,

Thank you for your help but unfortunately, it’s not working. The sound continuously stays on when the trail starts :frowning:

kindest regards,
Gull

You have to use code to do this. You first need to set up the sound.

In a Begin Experiment tab

correct = sound.Sound(1245, secs = 0.2, hamming=False)
correct.setVolume(0.8)
correct_timer_started = ''
correct_timer = core.CountdownTimer(start = 0.2)

You can change the frequency (1245), duration (0.2 s), and volume (0.8) that are set here. Then in a code component within your trial on the Every Frame tab, wait for a left keypress (or change to whatever you want) and play the sound for the desired duration (0.2).

if 'left' in key_resp.keys and not correct_started:
    correct.play()
    correct_started = True
    correct_start_time = globalClock.getTime()
    if correct_started and not correct_ended:
        if globalClock.getTime() - correct_start_time >= 0.2:
            correct_ended = True
            correct_started = False

This will make sure the sound only plays for the desired time, here 0.2 s. You’ll also need to initialize the correct_started and correct_ended variables in a Begin Routine tab

correct_started = False
correct_ended = False

Hello gull,

the sound ends in my experiment. Did you limit the duration of the sound? You know it is rather difficult to help you properly if you don’t post the relevant part or your experiment.

Best wishes Jens

SoundKey.psyexp (9.7 KB)