I am doing an experiment and I want a part to be shown if a certain key ("b") is pressed and skipped, if any other key is pressed. The key that is pressed is saved in key_resp_3.keys and if I visualize what's saved in the key_resp_3.keys variable it's simply b. So I thought this would be the right comparison:
if ((key_resp_3.keys != "b")) {
continueRoutine = false;
}
However in this case, the routine is always skipped so somehow "b" is obviously not the way the key is saved in the key_resp_3.keys variable. Does anyone have an idea what is the right comparison instead?
Thank you!
Hard to tell the root cause of the problem without the rest of the code. But you can simply use an event listener like below
document.addEventListener('keydown', function(event) {
console.log(event.key)
const key = event.key
if(key == "b")
alert("b presed")
});
Or can it be as simple as you are trying to set const key_resp_3 = event.key then checking in the if key_resp_3.keys so you are trying to access variable.key.key
In this special case:
since the keys is a array you can use
Does contains b
key_resp_3.keys.includes('b')
Does not contains b
!key_resp_3.keys.includes('b')