Increasing rotation speed of object when key is held down

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:

  1. How to increase keypress speed?
  2. 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:

Hi FJBorn

I would think a minimal adjustment would do the trick here

Set the initial keySpeed e.g. keySpeed = 0.1 and the desired increase speed per frame increasePerFrame = 0.001 at the Begin of the Routine.
Then do something like

keys = adjust_keyboard.getKeys(waitRelease=False, clear=False)
if len(keys) > 0:
        key = keys[len(keys)-1] # get recent keypress
        if not key.duration:
            keySpeed+= increasePerFrame #This is like acceleration speed so you could think of making some non-linear function
            if keySpeed > 0.5:
                    keySpeed = 0.5
            if key.name == "left":
                    this_ori -= keySpeed
            if key.name == "right":
                    this_ori += keySpeed
        else: # Meaning PressedKey was released
               keySpeed = 0.1 #Reset KeySpeed
                 
                    

Note: If you want to have some non-linear acceleration of Keyspeed. Use increasePerFrame = 1.001 and keySpeed*= increasePerFrame. So you have an 0.1 % Increase of keySpeed per Frame.

Dear @Luke,

Thank you very much, this is exactly what I was looking for and I am very thankful that you replied in almost no time. Online it works already perfectly fine and your code (auto-translated to JS) does the trick.

When I run it offline in Python, however, it does something weird. You can adjust the stimulus in one direction, but then it kind of freezes and pressing the arrow buttons does not result in any movement anymore. Btw, the same happened to me when implementing the code from your continuousMousePressShowcase demo. Any idea what could be the reason for this?

Thank you!

Yes, the Demo was mainly targeted for Online-Use. The iohub-backend in the used PsychoPy Version could be the problem. You could update to 2022.2.1 and look if it is working or just change the keyboard backend to “PTB”.

Thank you very much, Luke!