# Space bar to continue routine, only after a button press

**URL:** <https://discourse.psychopy.org/t/space-bar-to-continue-routine-only-after-a-button-press/26148>\
**Category:** Builder\
**Created:** [November 23, 2021, 7:27pm UTC](https://discourse.psychopy.org/t/space-bar-to-continue-routine-only-after-a-button-press/26148 "2021-11-23T19:27:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexSwiderski](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alexswiderski/32/31131_2.png) [@AlexSwiderski](https://discourse.psychopy.org/u/AlexSwiderski)\
**Post date:** [November 23, 2021, 7:27pm UTC](https://discourse.psychopy.org/t/space-bar-to-continue-routine-only-after-a-button-press/26148/1 "2021-11-23T19:27:06Z")

</div>

Hi all, im using psychoPy builder for an experiment where the participant can press either 1 or 2 when an image appears. Then, they must press space bar to continue to the next item in the routine. The problem of my current setup is that space bar allows to continue regardless if a choice has been made, and I want the button press of 1 or 2 to be required to continue.

I found a nice exakmple with this experiment that uses a rating scale [here](https://discourse.psychopy.org/t/recording-a-correct-answer-from-a-rating-scale/2712); however, I am not sure how to apply this to my example.

Like the user in the above link, I am using a keyboard component for this, which is apparently not the right way to go about it.

Is it possible to tell psychopy to only continue the routine if a 1 or 2 has been pressed, by pressing spacebar? Second, is it possible to this and have only the last key stroke of 1 or 2 be passed on to my data output?

---

<div class="post-metadata">

**Author:** ![AlexSwiderski](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alexswiderski/32/31131_2.png) [@AlexSwiderski](https://discourse.psychopy.org/u/AlexSwiderski)\
**Post date:** [November 30, 2021, 5:59pm UTC](https://discourse.psychopy.org/t/space-bar-to-continue-routine-only-after-a-button-press/26148/2 "2021-11-30T17:59:19Z")

</div>

Hi all, bumping this post up. Is there any other information that would be helpful to get some help on this problem?

Sincerely,

Alex

---

<div class="post-metadata">

**Author:** ![jtseng](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jtseng/32/32082_2.png) [@jtseng](https://discourse.psychopy.org/u/jtseng)\
**Post date:** [November 30, 2021, 9:11pm UTC](https://discourse.psychopy.org/t/space-bar-to-continue-routine-only-after-a-button-press/26148/3 "2021-11-30T21:11:40Z")

</div>

Hi there - if you’re using Builder, the following components work for me:

- Image component for the image with an infinite duration
- Keyboard component named `key_resp` (or whatever other name) with:
  - An infinite duration
  - Unchecked ‘Force end of Routine’ as this would make any of the allowed keypresses end the trial
  - `'1', '2'` as the ‘Allowed Keys’

- A code component under the ‘Each Frame’ tab (as you want to run this code during the routine, on each frame, as per the documentation [here](https://www.psychopy.org/builder/components/code.html#end-a-loop-early)) with the following snippet:

```python
response = key_resp.getKeys() # grab keypresses
if '1' in key_resp.keys or '2' in key_resp.keys: # first check
    if response and 'space' in response: # next, check subsequent spacebar press
        continueRoutine = False # end routine

```

Hope that helps.

---

<div class="post-metadata">

**Author:** ![AlexSwiderski](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alexswiderski/32/31131_2.png) [@AlexSwiderski](https://discourse.psychopy.org/u/AlexSwiderski)\
**Post date:** [December 2, 2021, 8:09pm UTC](https://discourse.psychopy.org/t/space-bar-to-continue-routine-only-after-a-button-press/26148/4 "2021-12-02T20:09:35Z")

</div>

Thank you sooooo much! This did the trick 😃
