I want to do the BART task (pumping up a balloon with pressing space til it bursts) on Pavlovia but have an issue with the space button being pressed. I want to pump up the balloon and therefore need seperated space presses to pump it up. When I run it locally on PsychoPy, that works. But it seems that with the Java Script I have, it does not stop when space is pressed but you can just leave down the finger and it pumps up the baloon till it pops. Now this biases my results and the whole idea of the game… this is the following java script i use for this:
var thisResp = psychoJS.eventManager.getKeys();
var spacePressed = thisResp.includes(“space”);
// Debugging output
console.log(thisResp: ${thisResp});
console.log(spacePressed: ${spacePressed}, prevSpacePressed: ${prevSpacePressed});
if (spacePressed && !prevSpacePressed) { // Detect space press only if it was not pressed before
nPumps += 1;
console.log(nPumps: ${nPumps}, maxPumps: ${maxPumps}); // Debugging output
if (nPumps > maxPumps) {
popped = true;
continueRoutine = false;
}
}
if (thisResp.includes(“return”)) {
popped = false;
continueRoutine = false;
}
prevSpacePressed = spacePressed; // Update the previous space pressed state
Use a combination of keydown and keyup events to determine if you need to pump the balloon. On keydown you would trigger the pumping of the balloon and set a flag (your prevSpacePressed) value to say it was pressed (aka true). Then on the keyup event you would reset this flag back to false.
I tried this modified code, but it also did not work…:
var spacePressed = false;
var prevSpacePressed = false;
document.addEventListener(“keydown”, function(event) {
if (event.key === " ") { // Check if the spacebar is pressed
spacePressed = true;
}
});
document.addEventListener(“keyup”, function(event) {
if (event.key === " ") { // Check if the spacebar is released
spacePressed = false;
}
});
// Main routine
if (spacePressed && !prevSpacePressed) { // Detect space press only if it was not pressed before
nPumps += 1;
console.log(nPumps: $ {
nPumps
}, maxPumps: $ {
maxPumps
}); // Debugging output
if (nPumps > maxPumps) {
popped = true;
continueRoutine = false;
}
}
if (thisResp.includes("return")) {
popped = false;
continueRoutine = false;
}
prevSpacePressed = spacePressed; // Update the previous space pressed state