I am working on: MacOs Monterey, using PsychoPy version: v2021.2.3
Using the right and left arrow keys, I want to rotate an object clockwise and counterclockwise while incrementing its orientation variable by 1 degree per click. If I hold down the key, I want the object to rotate smoothly until the key is released. Last but not least (and here is where I need help): I’d like the rotation speed to increase when the key is pressed, but to go back to a slow speed when it’s released.
What did you try to make it work?:
I read and applied ideas from:
- How to increase keypress speed?
-
Move the slider maker with keys/mouse and record continuous ratings
and worked through the demo version of @wakecarter to move a dot on a slider (Pavlovia)
and and worked through the demo version of @Luke (Luke Boelling / continuousMousePressShowcase · GitLab)
What specifically went wrong when you tried that?:
In order to increase rotation speed, I introduced a speed variable that is multiplied with my increase in orientation. Due to the fact that this variable is not reset once the key is lifted, I experience excessive rotation steps (not rotation by 1 degree but 5 or 10 degrees per click). This makes it difficult for my participants to adjust the stimuli precisely. Here is where I need help.
My experiment needs to work online.
Many thanks in advance!
Here is the code:
keySpeed1 = 0 # initializing speed factor for orientation
keySpeed2 = 0 # initializing speed factor for orientation
keys = adjust_keyboard.getKeys(waitRelease=False, clear=False)
if len(keys) > 0:
key = keys[len(keys)-1] # get recent keypress
if not key.duration:
print(key.duration)
if key.name == "left":
this_ori -= 1*keySpeed1 # this_ori is my variable that determines the
print(this_ori) # orientation of my object-
if keySpeed < 3:
keySpeed += 0.5
if keySpeed > 3:
keySpeed = 0.5
if key.name == "right":
this_ori += 1 * keySpeed2
print(this_ori)
if keySpeed < 3:
keySpeed += 0.5
if keySpeed > 3:
keySpeed = 0.5
Here is a picture of my code: