psychopy.org | Reference | Downloads | Github

Recording keyboard responses

OS : macOS Mojave
PsychoPy version : v2020.1.2
Standard Standalone? Yes
What are you trying to achieve?:
Record five keyboard press. And present live feedback on key presses. End routine when 5 keys have been pressed.

What did you try to make it work?:
I have a builder keyboard component named ‘key_resp’ and a text component named ‘text’ to display the live feedback text.

The following is on ‘each frame’ of the code component.

if key_resp.status == STARTED and not waitOnFlip:
        if (len(_key_resp_allKeys)) >= 5:
                core.checkPygletDuringWait = False
                core.wait(0.3)
                continueRoutine = False
       
        theseKeys = key_resp.getKeys(keyList=alphabets, waitRelease=False)
        _key_resp_allKeys.extend(theseKeys)
        
        if len(_key_resp_allKeys):
            key_resp.keys = [key.name for key in _key_resp_allKeys]  # storing all keys
            key_resp.rt = [key.rt for key in _key_resp_allKeys]

            if len(theseKeys):
                #typedWord = typedWord + key_resp.keys[-1] 
                #typedWord = typedWord + theseKeys[0].name
                typedWord = typedWord + key_resp.keys[abs(len(_key_resp_allKeys)-1)]
                
                text.text=typedWord

What specifically went wrong when you tried that?:
This largely works.
key_resp.keys = [key.name for key in _key_resp_allKeys]
This line above seems to capture all the keys typed, even when typed really quickly.

However,

#typedWord = typedWord + key_resp.keys[-1] 
#typedWord = typedWord + theseKeys[0].name
typedWord = typedWord + key_resp.keys[abs(len(_key_resp_allKeys)-1)] 

All three options here for joining the keys presses to ‘typedWord’ seem to miss some keys if they were typed simultaneously or in very quick succession, ~100ms. I need the keys here to join as a string as supposed to a list. Can anyone suggest a fix?

Minor 2nd Question: I’m trying to stop collecting keyboard responses at the 5th keypress. If I typed (more than 5 keys) really quickly, sometimes it is not able to endRoutine before it realises that there have been 5 key presses. At the same time, I also needed the live feedback to display all 5 key presses before the routine ended. Hence the 300ms wait.

We intend to eventually have a Pavlovia version of this experiment.

Some examples of the problem in the output file:
typedWord // key_resp// key_rt
roya // [‘r’, ‘o’, ‘y’, ‘a’, ‘l’] // [0.7027494410285726, 0.8150179670192301, 1.0176382460049354, 1.0513858360354789, 1.2200069150421768]

mids // [‘m’, ‘i’, ‘d’, ‘t’, ‘s’] // [0.5428984200116247, 0.7229219200089574, 0.779157450015191, 1.0041551369940862, 1.0155205769697204]

ears // [‘y’, ‘e’, ‘a’, ‘r’, ‘s’] // [0.869883059989661, 0.9486287219915539, 1.1286048580077477, 1.2187204079818912, 1.376174220989924]

ksjhi // [‘k’, ‘s’, ‘d’, ‘j’, ‘h’, ‘e’, ‘r’, ‘h’, ‘o’, ‘i’] // [2.2272050339961424, 2.2383754740003496, 2.2496499109547585, 2.2612062619882636, 2.3284927969798446, 2.4296111239818856, 2.440830361971166, 2.5308294039568864, 2.609585241996683, 2.633057287952397]