新代码禁用使用转义退出的功能



我添加了以下代码来移动屏幕上的刺激,我只是注意到这似乎禁用了在脚本中任何地方使用"转义"键转义代码的功能。我对Javascript仍然很陌生,任何指示将不胜感激。

//the following appears a few time throughout the script 
// check for quit (typically the Esc key)
if (psychoJS.experiment.experimentEnded ||
psychoJS.eventManager.getKeys({ keyList: ['escape'] }).length > 0) {
return quitPsychoJS('The [Escape] key was pressed. Goodbye!', false);
}
// and then later in the script
// FRR ----- manual entry BEGIN; copy this before "function TestRoutineBegin(trials) {" 
// code to move stimulus
var xforce;
spacepressed = 0; // reset each trial
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
//psychoJS.eventManager.clearEvents({eventType:'keyboard'});
var thisResp = psychoJS.eventManager.getKeys();
console.log(thisResp);
if (spacepressed == 0) {
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]);
}
if ('space' == thisResp[0]) {
LocCol2 = 'white';  // if response locked with spacebar
//document.removeEventListener("keydown", getKeyResponse); //once spacebar is pressed, dont except any more input
spacepressed = 1;//new 28.05 15:55
}
/* if (psychoJS.eventManager.getKeys({keyList:['space']}).length > 0) {
LocCol2 = 'white'  // if response locked with spacebar
spacepressed = 1;
}*/
psychoJS.eventManager.clearEvents({ eventType: 'keyboard' });
}
}
window.addEventListener('keydown', getKeyResponse); //tried keydown as well, with the same problem
window.addEventListener('keyup', resetXforce);
//  FRR------------------------------------------------------------------------------

getKeyResponse()函数似乎总是清除所有键盘事件:

function getKeyResponse (event) {
...
if (spacepressed == 0) {
...
psychoJS.eventManager.clearEvents({ eventType: 'keyboard' });
}
}

我会在该函数的开头添加对转义键的检查:

function getKeyResponse (event) {
...
// new addition
if (psychoJS.eventManager.getKeys({ keyList: ['escape'] }).length > 0) {
return;
}
if (spacepressed == 0) {
...
psychoJS.eventManager.clearEvents({ eventType: 'keyboard' });
}
}

最新更新