Hi All,
I have a feature in my design (for a rating slider) where the user moves the slider marker by pressing keys. Moving the marker by an increment for each key press was too slow, moving it smoothly while the key is pressed was not precise enough, so I combined the two methods and assigned one key for smooth movement and one for gradual movement. So in the code snippet below ‘a’ moves the marker smoothly to the left and ‘f’ to the right, (keysA) and ‘s’ moves the marker to the left and ‘d’ to the right by one (keysB). ‘Enter’ confirms the selection and breaks the while loop.
At the same time, I would like to keep a record of all the keys pressed (not just the assigned ones). Unfortunately, when I add another getKeys() ‘s’ and ‘d’ are not being recorded, I assume because they are assigned in keysB and getting cleared from the buffer? Enter (‘return’) is also not being recorded for some reason. Does anybody know how I could remedy this?
Extra credit question
Ideally, I would like to achieve different movement speeds with the same keys, so one press of ‘s’ would move the marker by one increment, but if it’s pressed down it would move the marker quickly. Like if you press left and right arrows in a word document. Can anyone think of a way to code this?
I would be madly appreciative if anyone has any suggestions
from psychopy import visual, monitors
from psychopy.hardware import keyboard
kb = keyboard.Keyboard()
mon = monitors.Monitor('myMonitor', width=35.56, distance=60)
win = visual.Window(monitor=mon, fullscr=False, size=[500,500], pos=[0,0])
markerStim = visual.Rect(win=win, size=(0.01, 0.2), colorSpace = 'rgb255', fillColor = [147,0,0], pos = [0,0])
kb.clearEvents()
while True:
markerStim.draw()
win.flip()
allKeys = kb.getKeys(None, waitRelease=True, clear = False)
keysA = kb.getKeys(['a', 'f', 'return'], waitRelease=False, clear = False)
keysB = kb.getKeys(['s', 'd'], waitRelease=False, clear = True)
if keysA and not keysA[-1].duration:
keyA = keysA[-1].name
if keyA == 'a':
markerStim.pos -= [0.01, 0]
elif keyA == 'f':
markerStim.pos += [0.01, 0]
elif keyA == 'return':
break
if keysB:
for k in keysB:
keyB = keysB[-1].name
if keyB == 's':
markerStim.pos -= [0.01, 0]
elif keyB == 'd':
markerStim.pos += [0.01, 0]
win.close()