# Auto-response script in Psychopy

**URL:** <https://discourse.psychopy.org/t/auto-response-script-in-psychopy/19349>\
**Category:** Coding\
**Created:** [January 4, 2021, 9:25am UTC](https://discourse.psychopy.org/t/auto-response-script-in-psychopy/19349 "2021-01-04T09:25:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![filippogambarota](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/filippogambarota/32/8131_2.png) [@filippogambarota](https://discourse.psychopy.org/u/filippogambarota)\
**Post date:** [January 4, 2021, 9:25am UTC](https://discourse.psychopy.org/t/auto-response-script-in-psychopy/19349/1 "2021-01-04T09:25:04Z")

</div>

Hi,  
I would like to know if could be possible to write a script that automatically press a key or a mouse button in order to simulate an experiment and debug in real-life the code.  
Could it be possible?

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [January 6, 2021, 10:13am UTC](https://discourse.psychopy.org/t/auto-response-script-in-psychopy/19349/2 "2021-01-06T10:13:53Z")

</div>

Hi,

Would something like this work for what you had in mind?

1. make a class to generate a simulated key press response:

```auto

class simKeys:
    '''
    an object to simulate key presses
    
    keyList: a list of keys to watch
    name: randomly selected from keyList
    rtRange: [min RT, max RT] where min and max RT are sepecified in ms
   rt: randomly selected from rtRange
        
    '''
    def __init__ (self, keyList, rtRange):
        self.name=np.random.choice(keyList)
        self.rt = np.random.choice(np.linspace(rtRange[0], rtRange[1])/1000)

```

1. at the start of your routine make a simulated key object:

```auto
thisSimKey=simKeys(keyList=['left', 'right'], 
    rtRange=[200,1000])

```

1. When the rt of that simulated keypress is reached add it to your key response object so that the routine is ended

```auto
if t >= thisSimKey.rt:
    _key_resp_allKeys.extend([thisSimKey])

```

Here is an associated demo file [simulate\_response.psyexp](https://discourse.psychopy.org/uploads/short-url/8ERABmVWWqCBvIUUFmbtUZCuYyr.psyexp) (14.1 KB)

Hope this helps,  
Becca

---

<div class="post-metadata">

**Author:** ![filippogambarota](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/filippogambarota/32/8131_2.png) [@filippogambarota](https://discourse.psychopy.org/u/filippogambarota)\
**Post date:** [February 10, 2021, 6:24pm UTC](https://discourse.psychopy.org/t/auto-response-script-in-psychopy/19349/3 "2021-02-10T18:24:57Z")

</div>

Hi Becca! thank you this works like a charm! do you think that is possible to implement this using mouse press (emulate the response not move the mouse) even online?

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [February 12, 2021, 10:07am UTC](https://discourse.psychopy.org/t/auto-response-script-in-psychopy/19349/4 "2021-02-12T10:07:46Z")

</div>

Hi There,

This currently wouldn’t work online/with mouse clicks but could easily be adapted to!

For the online part, this currently uses the numpy library for choice and linspace - so would need to implement the JS equivalent of those.

For the mouse click part the framework would be near identical, only with simulating a click rather than a key press.

I have a feeling this would make a nice demo for psychopy but could take a bit more work (so I am bookmarking this thread to revisit in the future). But, in the meantime if you have developments in extending the demo please do share those!

Becca
