Duration of key is always None

OS (e.g. Win10):
PsychoPy version (2021.2.0):
**Standard Standalone? y
**What are you trying to achieve?:

I want to know how a long a key was held down for before being released

**What did you try to make it work?:

For any given key I called for the key.duration attribute

**What specifically went wrong when you tried that?:

That attribute is always None

See below the code I am using inside the Each Frames tab

print(_key_mod_allKeys)
if len(_key_mod_allKeys):
    print(_key_mod_allKeys[-1].name)
    print(_key_mod_allKeys[-1].tDown)
    print(_key_mod_allKeys[-1].duration)

And then some output

[<psychopy.hardware.keyboard.KeyPress object at 0x0000019722DABFD0>, <psychopy.hardware.keyboard.KeyPress object at 0x0000019722D9F160>]
right
281912.6402972
None
[<psychopy.hardware.keyboard.KeyPress object at 0x0000019722DABFD0>, <psychopy.hardware.keyboard.KeyPress object at 0x0000019722D9F160>]
right
281912.6402972
None
[<psychopy.hardware.keyboard.KeyPress object at 0x0000019722DABFD0>, <psychopy.hardware.keyboard.KeyPress object at 0x0000019722D9F160>, <psychopy.hardware.keyboard.KeyPress object at 0x0000019722D9F240>]
left
281912.7530118
None

You can see in this part of the output that I have just pressed a new key (the left key), having previously pressed the right key. I released the right key, and therefore the key.duration should not be None.

2 Likes

Hi There,

Do you have the keyboard set to end on press? this might be helpful too Measuring key lift time relative to stimulus onset - #3 by Becca

Becca

Hi Becca,

Thanks again for helping me out!

I donā€™t believe so, though Iā€™m not certain. I see an option for ā€œForce end of routineā€ that is not checked. Is that what you mean?

The link you shared is actually particularly prescient, since I will be deploying this experiment on Pavlovia as soon as this bug is worked out. However, if it is still the case for PsychoJS that I will need to manually write code rather than using something from the builder than I can push now with no code and then just go at it online. What are your thoughts on this?

Thanks again,

Peter

Hi Peter,

Aha ok if you are going online what I would suggest is still make your experiment in builder but use code components to monitor the key press - a bit like in the file shared in this post !

Becca

Okay, Iā€™ll do that.

Thank you so much!

Peter

Iā€™m having exactly this problem: key.duration remains None with pyglet even after the key is released. Unfortunately I cannot switch to PTB backend which does not seem to work with my MRI-compatible button box (cf. Buttonbox delay - Programming Help - Psychtoolbox).
Is there another way of checking whether a key has been released that would work even with pyglet?