I want to have a code component under each frame, when I hold down the right-left arrow keys it will increment/decrement a variable. This will be used to rotate a line real time. At my current setup you need to constantly press and release, I want to make it so that as long as I hold down the key, the orientation will be updated. By the way, I’d appreciate a method which would also work online.
keys = event.getKeys(keyList=['left','right'])
if 'left' in keys:
rot_line.ori += 2.0
elif 'right' in keys:
rot_line.ori -= 2.0
The event module can’t give you information about when a key is held down or released, only when it is initially pressed. So you need to shift to the newer Keyboard class:
You are re-creating and resetting the keyboard object on every frame, which will really muck with trying to detect if a key has been pressed before. Instead, this code should be in the “Begin routine” tab (so that it only happens once per trial), rather than in the “Each frame” tab (so that it gets run over and over, at typically 60 Hz):
kb = keyboard.Keyboard()
# this is likely not even necessary, as I guess it
# is set to zero when the keyboard is created:
kb.clock.reset()
In terms of the rest of the code, I’m not too familiar with the Keyboard object, but I think you should set clear = False in the call to .getKeys(), or else you’ll have to keep pressing the key to have it be detected again.
You can’t put all of that code in the “Begin routine” tab: it means that the keyboard will be checked only once per trial, before the trial even starts.
As stated above, only this should be in the “Begin routine” tab:
kb = keyboard.Keyboard()
Everything else should be in the “Each frame” tab, so that it runs on every screen refresh.