Error when using event.getkeys() in latest version

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Win 10
PsychoPy version (e.g. 1.84.x): 2023.2.2

What are you trying to achieve?:
I am trying to use two keys to navigate left and right in a slider component. It worked perfectly in older versions with the following code:

Before Experiment:

import random

Begin Routine:

#this sets the slider to a random position in each trial
event.clearEvents('keyboard')
random_number = random.randint(1, 5)

slider.markerPos = random_number

Each Frame:

#this makes sure 'v' and 'b' can be used to navigate left and right

keys = event.getKeys()

if len(keys):
    if 'v' in keys:
        slider.markerPos = slider.markerPos - 1
       
    elif 'b' in keys:
        slider.markerPos = slider.markerPos + 1 

I tried running it in an older version of PsychoPy, but that did not take. I can still run the experiment and still continue from, the slider using a defined keypress, but I cannot move the slider anymore. Does anyone know, why that might be the case? Also, I am getting the following error messages (since I am not very prolific in programming, I will just paste them here without knowing whether they are important or not)

184.8842 INFO Loaded monitor calibration from [‘2023_01_20 15:01’]
C:\Program Files\PsychoPy\lib\site-packages\scipy_init_.py:146: UserWarning: A NumPy version >=1.16.5 and <1.23.0 is required for this version of SciPy (detected version 1.24.4
pygame 2.1.0 (SDL 2.0.16, Python 3.8.10)
warnings.warn(f"A NumPy version >={np_minversion} and <{np_maxversion}"
WARNING: pytables package not found. ioHub functionality will be disabled.
Hello from the pygame community. Contribute - pygame wiki
WARNING: pytables package not found. ioHub functionality will be disabled.

I’m not seeing a specific error message there.

If you add a text component with text set to $msg in each frame then you could change your code to

if len(keys):
    if 'v' in keys:
        msg = 'left'
        slider.markerPos = slider.markerPos - 1
    elif 'b' in keys:
        slider.markerPos = slider.markerPos + 1 
        msg = 'right'

Put msg = 'Waiting' in Begin Routine

If the message changes then it is a slider issue rather than a getKeys issue.

Thank you for the quick reply - I followed your suggestion, but unfortunately the message did not change when pressing the keys, is there any way, I can convert this code, so it works with the newer keyboard()-command? Or should getkeys still work in the newer version?

Ok, I fiddled around and it worked with the following code:

Before Experiment:

from psychopy.hardware import keyboard
from psychopy import core
kb = keyboard.Keyboard()
#keys = event.getKeys()
keys = kb.getKeys(['v', 'b', 'n', 'Esc'], waitRelease=True)

if len(keys):
    if 'v' in keys:
        slider.markerPos = slider.markerPos - 1
    elif 'b' in keys:
        slider.markerPos = slider.markerPos + 1 

Glad you found a solution. However, event.getKeys does seem to work in 2023.2.3 (I’m testing a pre-release version). Does this experiment work for you?

event-getkeys.psyexp (11.4 KB)

I tried the experiment and it works, so it seems it might have been a problem elsewhere in the code I cant point to.