Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Messy keyboard reaction times with waitRelease=True #6853

Open
giuliagennari opened this issue Sep 15, 2024 · 0 comments
Open

[Bug]: Messy keyboard reaction times with waitRelease=True #6853

giuliagennari opened this issue Sep 15, 2024 · 0 comments
Labels
🐞 bug Issue describes a bug (crash or error) or undefined behavior.

Comments

@giuliagennari
Copy link

PsychoPy Version

2024.2.1

What OS are your PsychoPy running on?

Windows 11

Bug Description

In my experiment subjects are asked to press certain keys during one trial, hold them down, and release them in one of the following trials. I need to store their reaction times and the duration of every key press.

If the key is pressed and released within trial n, all good.

When the key is released during trial n+1 , the key event is logged in the row corresponding to trial n+1 but the reaction time refers to trial n

Expected Behaviour

Positive reaction times when the key is pressed and released within trial n, negative reaction times when the key is pressed in trial n but released in trial n+1.

NOTE: this was working as expected in version 2022.2.1 (I am upgrading to exploit new features!)

Steps to Reproduce

Create a code component with these snippets. Press (one of) the specified key(s) during one trial, release during the next.
The expected reaction times are stored in CustomKeyboard.rt_corr (and correspond to CustomKeyboard.rt.rt when keys are pressed and released within the same trial).

# ** BEGIN EXPERIMENT tab ** #

CustomKeyboard = keyboard.Keyboard() 

# ** BEGIN ROUTINE tab ** #

## create starting attributes for CustomKeyboard
CustomKeyboard.keys = []
CustomKeyboard.rt = []
CustomKeyboard.tDown = []
CustomKeyboard.rt_corr = []
CustomKeyboard.duration = []
_CustomKeyboard_allKeys = []

CustomKeyboard.tStart = None
CustomKeyboard.tStartRefresh = None
CustomKeyboard.status = NOT_STARTED

# ** EACH FRAME tab ** #

## *CustomKeyboard* updates

waitOnFlip = False
# if CustomKeyboard is starting this frame...
if CustomKeyboard.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
    # keep track of start time/frame for later
    CustomKeyboard.frameNStart = frameN  # exact frame index
    CustomKeyboard.tStart = t  # local t and not account for scr refresh
    CustomKeyboard.tStartRefresh = tThisFlipGlobal  # on global time
    win.timeOnFlip(CustomKeyboard, 'tStartRefresh')  # time at next scr refresh
    # add timestamp to datafile
    thisExp.timestampOnFlip(win, 'CustomKeyboard.started')
    # update status
    CustomKeyboard.status = STARTED
    # keyboard checking is just starting
    waitOnFlip = True
    win.callOnFlip(CustomKeyboard.clock.reset)  # t=0 on next screen flip
    # skipping the line below doesn't change the issue
    win.callOnFlip(CustomKeyboard.clearEvents, eventType='keyboard')  

if CustomKeyboard.status == STARTED and not waitOnFlip:
    theseKeys = CustomKeyboard.getKeys(keyList=['capslock','a','return','pound'], waitRelease=True)
    _CustomKeyboard_allKeys.extend(theseKeys)
    if len(_CustomKeyboard_allKeys):
        CustomKeyboard.keys = [key.name for key in _CustomKeyboard_allKeys]  # storing all keys
        CustomKeyboard.rt = [key.rt for key in _CustomKeyboard_allKeys]
        CustomKeyboard.tDown = [key.tDown for key in _CustomKeyboard_allKeys]
        CustomKeyboard.rt_corr = [ key.tDown-CustomKeyboard.tStartRefresh for key in _CustomKeyboard_allKeys]
        CustomKeyboard.duration = [key.duration for key in _CustomKeyboard_allKeys]

# ** END ROUTINE tab ** #

## check for responses

if CustomKeyboard.keys in ['', [], None]:  # No response was made
    CustomKeyboard.keys = None

thisExp.addData('CustomKeyboard.keys',CustomKeyboard.keys)
if CustomKeyboard.keys != None:  # we had a response
    thisExp.addData('CustomKeyboard.rt', CustomKeyboard.rt)
    thisExp.addData('CustomKeyboard.tDown', CustomKeyboard.tDown)
    thisExp.addData('CustomKeyboard.rt_corr', CustomKeyboard.rt_corr)
    thisExp.addData('CustomKeyboard.duration', CustomKeyboard.duration)

```python

### Additional context

_No response_
@giuliagennari giuliagennari added the 🐞 bug Issue describes a bug (crash or error) or undefined behavior. label Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Issue describes a bug (crash or error) or undefined behavior.
Projects
None yet
Development

No branches or pull requests

1 participant