psychopy.org | Reference | Downloads | Github

Continuously moving object using psychoJS

URL of experiment:

Description of the problem:
I have trouble making moving object using psychoJS.

What I want to make is an object (e.g., circle) that is movable using keyboard.

I have done this in the offline experiment using pyglet library as below:

import pyglet
from pyglet.win import key
keyState=key.KeyStateHandler()
win.winHandle.push_handlers(keyState)

if keyState[key.RIGHT]:
position+=speed
elif keyState[key.LEFT]:
position-=speed

However, it seems that psychoJS does not contain libraries similar to pyglet

I tried to make code in javascript that can receive current Keyboard state, however, I was not successful until now.
(getKeys in the keyboard library seems to detect keyboard input but it seems that it does not detect
continuous keyboard press input…)

I would appreciate any comment on this, thanks!

Why are you using pyglet calls rather than PsychoPy’s own APIs?

https://www.psychopy.org/api/hardware/keyboard.html

You’d be better off going that route and seeing how far the auto translate option to JavaScript gets you. It won’t be able to do much with pyglet calls.

Thanks Michael!

I solved this problem by referring your comments in the

Have you been able to get this working online?

And if so, how?

Actually, I failed to implement smoothly moving object using current psychoJS keyboard.
Therefore, I tried to use keyboardJS library of the javascript by importing in javascript code as beloe:

Initially add
import ‘./lib/keyboard.min.js’;

and for each frame
keyboardJS.bind(‘up’, (e) => {
ud_mov = 1;
}, (e) => {
ud_mov = 0;
});
keyboardJS.bind(‘down’, (e) => {
ud_mov = -1;
}, (e) => {
ud_mov = 0;
});
keyboardJS.bind(‘right’, (e) => {
rl_mov = 1;
}, (e) => {
rl_mov = 0;
});
keyboardJS.bind(‘left’, (e) => {
rl_mov = -1;
}, (e) => {
rl_mov = 0;
});

x +=rl_mov*speed;
y +=ud_mov*speed;

Then, it worked smoothly at least in the local debugging mode of psychoJS.
However, I did not test it in the actual online experiment.

1 Like

Thank you. It’s worth giving a try.

Since I couldn’t get the keyboard work online, I was about to convert the .py file to .exe and send it out to participants like that, instead of using psychoJs.

This keyboardJS library is supposed to be installed by the user, right? I downloaded it and placed inside the “lib” folder. However when you sync the library won’t be uploaded to gitlab. You need to fix this by commenting them out in the .gitignore (found in your project repository) then it can be uploaded.

However, even after uploading it, the library can’t be loaded to Pavlovia. I think Pavlovia only allows the usage of some specific libraries.