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

问题描述 投票:0回答:1

我添加了以下代码以在屏幕上移动刺激,并且我刚刚注意到,这似乎禁用了在脚本中的任何位置使用“ escape”键对代码进行转义的功能。我对Java还是很陌生,任何指针都将不胜感激。

//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------------------------------------------------------------------------------
javascript psychopy
1个回答
0
投票

[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' });
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.