# Adding sound on key press without ending the trial

**URL:** <https://discourse.psychopy.org/t/adding-sound-on-key-press-without-ending-the-trial/33727>\
**Category:** Builder\
**Created:** [March 9, 2023, 10:16am UTC](https://discourse.psychopy.org/t/adding-sound-on-key-press-without-ending-the-trial/33727 "2023-03-09T10:16:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gull](https://avatars.discourse-cdn.com/v4/letter/g/aca169/32.png) [@gull](https://discourse.psychopy.org/u/gull)\
**Post date:** [March 9, 2023, 10:16am UTC](https://discourse.psychopy.org/t/adding-sound-on-key-press-without-ending-the-trial/33727/1 "2023-03-09T10:16:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [March 9, 2023, 11:27am UTC](https://discourse.psychopy.org/t/adding-sound-on-key-press-without-ending-the-trial/33727/2 "2023-03-09T11:27:01Z")

</div>

Hello gull

well, that should be possible. For instance add

```auto
$'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

---

<div class="post-metadata">

**Author:** ![gull](https://avatars.discourse-cdn.com/v4/letter/g/aca169/32.png) [@gull](https://discourse.psychopy.org/u/gull)\
**Post date:** [March 9, 2023, 3:35pm UTC](https://discourse.psychopy.org/t/adding-sound-on-key-press-without-ending-the-trial/33727/3 "2023-03-09T15:35:55Z")

</div>

Hello Jens,

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

kindest regards,  
Gull

---

<div class="post-metadata">

**Author:** ![Joseph\_Orr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/joseph_orr/32/6669_2.png) [@Joseph\_Orr](https://discourse.psychopy.org/u/Joseph_Orr)\
**Post date:** [March 9, 2023, 8:20pm UTC](https://discourse.psychopy.org/t/adding-sound-on-key-press-without-ending-the-trial/33727/4 "2023-03-09T20:20:53Z")

</div>

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

In a Begin Experiment tab

```auto
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).

```auto
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

```auto
correct_started = False
correct_ended = False

```

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [March 10, 2023, 7:44am UTC](https://discourse.psychopy.org/t/adding-sound-on-key-press-without-ending-the-trial/33727/5 "2023-03-10T07:44:23Z")

</div>

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](https://discourse.psychopy.org/uploads/short-url/eS2logY9pOsGE7BJOl1lcox1Yyp.psyexp) (9.7 KB)  
[https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/2/6/262b043360ef544f29bb7ed9fbd7d07da17e3b7f.wav](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/2/6/262b043360ef544f29bb7ed9fbd7d07da17e3b7f.wav)
