| Reference | Downloads | Github

Moving object with key press on Psychopy and Pavlovia

Hello all,

Is there a way to move an object continuously across the screen while holding down a key that will also work online on Pavlovia?

At the moment I have the following code which requires that you keep pressing the key - it won’t keep moving the object if you hold down the key. In my task the participant will be reporting the location of an object positioned around a circle. ‘obj1posX’ and ‘obj1posY’ are the position variables set in the task builder to refresh each frame.

I have been able to get this working using pyglet but cannot use that on Pavlovia.

keys = event.getKeys()

if keys:
   if 'left' in keys:
       deg = deg + 1
       obj1posX = Eccentricity * cos(math.radians(deg))
       obj1posY = Eccentricity * sin(math.radians(deg))
   if 'right' in keys:
       deg = deg -1
       obj1posX = Eccentricity * cos(math.radians(deg))
       obj1posY = Eccentricity * sin(math.radians(deg))

I have also tried the solution here:

But it doesn’t seem as though the keyboard class can be used online

Hi There,

Pleased you found a solution, but you are correct this is perhaps not the easiest solution to get online. Have you considered a mouse click instead?

Here is a demo where a triangle moves whilst the participant is pressing an onscreen button

you can fork the code here


1 Like

Thanks for sharing this demo! I agree that a mouse seems like a good alternative.
Ideally, I would want to use the keys though because I am trying to replicate an offline experiment that was built in Matlab.

If it’s just two keys, then I’d definitely go with the left and right mouse button. You wouldn’t need to care about the location of the mouse pointer so it would effectively still be a key press.

1 Like

Yes, I agree that would be quite similar to using the left and right arrow keys. I will implement that using Becca’s code above. Thanks for your help!