# Coding Keypress for Pause Function

**URL:** <https://discourse.psychopy.org/t/coding-keypress-for-pause-function/25772>\
**Category:** Coding\
**Created:** [November 3, 2021, 7:57am UTC](https://discourse.psychopy.org/t/coding-keypress-for-pause-function/25772 "2021-11-03T07:57:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zun19](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/zun19/32/4319_2.png) [@Zun19](https://discourse.psychopy.org/u/Zun19)\
**Post date:** [November 3, 2021, 7:57am UTC](https://discourse.psychopy.org/t/coding-keypress-for-pause-function/25772/1 "2021-11-03T07:57:46Z")

</div>

Hello, I am trying to link a keypress to a pause/play function using coding so that it would get recorded with timestamps in the output. I would like that whenever someone presses ‘p’, a movie stimulus then pauses or continues playing related to its current status. Although the pause/play function worked (in a past version of the coding below), the timestamps for those keypress were never recorded in the output files nor the movie statuses. I would like to make it so that the keypresses for p are recorded.

I feel like this coding below is getting me close to what I want, but it has crashed my system. Once I press pause, everything stops advancing and I have to escape.

moviekey is a keyboard component put in through builder and the allowed keys are ‘y’ and ‘p’.

I think that having the ‘p’ key in two places: coding and in a keyboard component is what is messing with system. I don’t know how to exactly fix that so any help would be great!

```auto
from psychopy.constants import (PLAYING, PAUSED)

# Check for action keys.....
for key in moviekey.keys:
    if key in ['p']:
        if mov.status == PLAYING:
            print('PAUSING')
            polygon.setAutoDraw(True)
            mov.pause()
            win.flip()
            if mov.status == PAUSED:
                print('UN-PAUSING')
                mov.play()
                polygon.setAutoDraw(False)

```

---

<div class="post-metadata">

**Author:** ![mjpvz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mjpvz/32/30962_2.png) [@mjpvz](https://discourse.psychopy.org/u/mjpvz)\
**Post date:** [November 5, 2021, 2:02pm UTC](https://discourse.psychopy.org/t/coding-keypress-for-pause-function/25772/2 "2021-11-05T14:02:47Z")

</div>

The indentation on the `if mov.status == PAUSED:` section appears to be wrong.

When you call `mov.pause()` the movie will be set to paued, meaning that the last if statment resolved to true, immediatly un-pausing the movie again.

I don’t think this causes the crash you mentioned, but still might be helpful
