Hi there,
In my current Pavlovia experiment, I can press the arrow keys to adjust the location of an image. But each key press only increases or decreases the position of the image by 4 pix. How can I make it so that when I press down the right key, the image would keep moving right until I release the right key?
I’ve tried using this solution, but it didn’t work.
https://discourse.psychopy.org/t/how-to-detect-whether-if-a-key-is-being-pressed-down/15830/15
I’ve also tried storing the keypress duration, then set the duration to null, as following, but it’s not working as the image doesn’t move when I press the arrow keys.
begin experiment (these code is for restricting the range of all the possible locations for the image)
x_basic = ((is_right)? 115: -115);
do{
bx = (Math.random() * 160) - 80;
by = (Math.random() * 160) - 80;
}while ((bx*bx) + (by*by) > 80*80);
startx = bx;
starty = by;
img_x = x_basic + bx;
img_y = y_basic + by;
old_key_length = 0;
begin routine
trial_resp.getKeys({"clear": true});
document.body.style.cursor='auto';
each frame
kp = trial_resp.getKeys({"waitRelease": false, "clear": false});
if ((kp.length > 0)) {
pressed = kp[kp.length-1].name;
duration = kp[kp.length-1].duration;
if ((pressed === "up"&& duration == null)) {
if ((bx*bx) + ((by+4)*(by+4)) <= 80*80){
by += 4;
img_y = y_basic + by;
}
} else if ((pressed === "down"&& duration == null)) {
if ((bx*bx) + ((by-4)*(by-4)) <= 80*80){
by -= 4;
img_y = y_basic + by;
}
} else if ((pressed === "left"&& duration == null)) {
if (((bx-4)*(bx-4)) + (by*by) <= 80*80){
bx -= 4;
img_x = x_basic + bx;
}
} else if ((pressed === "right"&& duration == null)) {
if (((bx+4)*(bx+4)) + (by*by) <= 80*80){
bx += 4;
img_x = x_basic + bx;
}
}
old_key_length = kp.length;
}
Thank you so much! Deeply appreciated!