psychopy icon indicating copy to clipboard operation
psychopy copied to clipboard

Possible unexpected and/or unintended behaviour of different keyboard backends for continuous keypresses with getKeys(waitRelease=False, clear=False) (ioHub, PsychToolBox, Pyglet) (e.g. missing key.duration in ioHub)

Open lboel opened this issue 2 years ago • 3 comments

Hi, I am currently working on a consistent method (online/offline) to move objects as long as a key is pressed. Using a straight forward approach by checking for an existing duration on a key in a "Store-All" keyboard component in the builder to check for keyPresses and keyReleases. The logic would be: As long as the last pressed key has no duration, it's likely not released yet. To get a fast impression here is a screenshot of the code component: image

See the whole demo file here: testKeyPressReleaseIohub.zip

This approach works fine and as expected (as documented) in PsychoJS:

https://user-images.githubusercontent.com/8046474/169765518-7e35fee4-f2b1-4701-99ab-a603a49c8506.mp4

tDown is returned on KeyPress, key duration is added then the Key is released. As expected

The documentation of Keyboard.getKeys (https://psychopy.org/_modules/psychopy/hardware/keyboard.html#Keyboard.getKeys) states:

waitRelease: bool (default True): If True then we won't report any "incomplete" keypress but all presses will then be given aduration. If False then all keys will be presses will be returned, but only those with a corresponding release will contain a durationvalue (others will haveduration=None But the current iohub.keyboard usage in hardware.keyboard results (at least on a Windows machine) in no durations at all for iohub backend in the same project. That id likely caused by an unexpected behaviour of the underlying iohub function "getPresses" here https://github.com/psychopy/psychopy/blob/9398a71beefa2cef30baaca7c1b5df34708ebfef/psychopy/hardware/keyboard.py#L309

The same code results here in no durations at all: https://user-images.githubusercontent.com/8046474/169767671-63f2f21e-94e3-4f8a-b3b8-aceb19ba46fb.mp4

For Iohub I will open a PR for a proposed fix.

In addition Psychtoolbox and Pyglet Keyboard backend produce two different unexpected outcomes in this simple demo: PsychToolBox: Is likely returning some Dict and not a series of pressed buttons and there is something wrong with tDown

https://user-images.githubusercontent.com/8046474/169769326-2dda172a-51cc-4975-8b75-6a442e89638d.mp4

Pyglet: Does not returned a stored list at all, escape is not working anymore and there are no durations.

https://user-images.githubusercontent.com/8046474/169769657-6ea4f6b7-852a-4950-984f-6f45b18abbe9.mp4

I think the iohub fix could be a fast way to improve the default setting behaviour and bring it more in line with the documentation.

lboel avatar May 23 '22 07:05 lboel

in #4846 an iohub-backend-fix proposed, but the behaviour of PsychToolBox and Pyglet Backend remains unexpected, and this could be referenced in the documentation after the PR is accepted (e.g. "waitRelease=False should be used with iohub-backend") and the other backends could be fixed later.

lboel avatar May 24 '22 06:05 lboel

I think your project highlights two issues with the PsychToolBox keyboard.Keyboard backend:

  1. Only the first press-release of a given key is returned if it has been pressed > 1 time since last event clear. For example pressing-releasing the 's' key twice before calling getKeys() only returns one 's' KeyPress event. Same occurs if getKeys(clear=False) is used and the same key is pressed, even if getKeys(clear=False) is being called repeatedly.
  2. tDown does seem to be using a different time offset vs. the other backends

@lboel, are these the same issues you are seeing when using the PsychToolBox Keyboard backend, or did you notice others? Thanks again

isolver avatar May 24 '22 13:05 isolver

Hi @isolver, I have just found time to check the ptb behavior. Please have a look at #4862 where I have implemented a fix for ptb that lead to the behavior the fixed iohub backend, psychojs and the documentation show. Feel free to edit the PR-branch. Maybe the sort in the ptb Keypress Return can be avoided. You could just pop the keypresses in _stillDown before returning the array. But just like the use of new "any"-Comparison, I am not really sure how a less intense check could account reliable for multiple keydowns at once and a release event of multiple keys at the same time.

lboel avatar May 27 '22 20:05 lboel

I believe this is now fixed for ptb and iohub, changes have been merged atleast. closing, please reopen if you are still having issues. Thank you

isolver avatar Jan 19 '23 13:01 isolver