总结问题: (对于js来说是新手-我试图在这里找到答案,但是不确定我是否不了解键盘行为或代码是否很奇怪。) 我正在尝试在屏幕上移动对象。该对象仅在按下另一个键后才移动,而不是对按下键或按下键做出反应(我都尝试过)。这对于对象移动来说很好,但是一旦按下空格键,我也尝试锁定响应,这同样行不通。
这是在Macbook Pro上使用Chrome版本81.0.4044.92
描述您尝试过的方法: 我尝试同时使用keyup和keydown。
在适当的时候显示一些代码:
var angle;
var myx;
var myy;
var LocCol;
var LocCol2;
var _resp_test_allKeys;
var TestComponents;
// FRR ----- manual entry BEGIN; copy this before "function TestRoutineBegin(trials) {"
// code to move stimulus
var xforce;
xforce = 0; // initially set speed ('force') to 0. It increases in steps of 1 as long as key is down
function resetXforce(event){
xforce = 0 // if finger has been lifted, go back to speed 0
}
function getKeyResponse(event){ // check for responses
var thisResp = psychoJS.eventManager.getKeys();
console.log(thisResp)
if ("left" == thisResp[0]) {
xforce--; // decrease angle with increasing acceleration the longer key pressed
angle = (angle + xforce); //plus not minus here since xforce may become negative
myx = (0.25 * Math.sin((((Math.PI * 2) * angle) / 360)));
myy = (0.25 * Math.cos((((Math.PI * 2) * angle) / 360)));
//smallcircle2.setPos([myx, myy]);
image_on_circ_test.setPos([myx, myy]);
}
if ("right" == thisResp[0]) {
xforce++; //increase angle with increasing acceleration the longer key pressed
angle = (angle + xforce);
myx = (0.25 * Math.sin((((Math.PI * 2) * angle) / 360)));
myy = (0.25 * Math.cos((((Math.PI * 2) * angle) / 360)));
//smallcircle2.setPos([myx, myy]);
image_on_circ_test.setPos([myx, myy]);
}
// for some reason this only responds on second keypress?
/* if ("space" == thisResp[0]) {
LocCol2 = 'white' // if response locked with spacebar
} */
if (psychoJS.eventManager.getKeys({keyList:['space']}).length > 0) {
LocCol2 = 'white' // if response locked with spacebar
}
//psychoJS.eventManager.clearEvents({eventType:'keyboard'});
}
document.addEventListener("keydown", getKeyResponse); //tried keydown as well, with the same problem
document.addEventListener("keyup", resetXforce);
// FRR ----- manual entry END -----