“Hello, I’m a beginner in Psychopy and I don’t understand much about programming. Is it possible to write a code that assigns the “z” and “m” keys on the keyboard to the left and right mouse clicks?”
Please could you clarify what you’re trying to achieve. Do you want someone to move the mouse but click with the keyboard? What are they clicking on?
“Hello, thank you for responding! I’m using Psychopy for the first time and programming the 2-BACK task. The task is simple: when a number appears on the screen, the participant will press the “Z” key for congruent and “M” for incongruent. However, due to other variables I will be collecting, the circumstances of my research require the participant to be in an orthostatic position, on top of a platform that will collect the oscillation of their center of gravity while performing the task, and the participant’s interaction with a keyboard may interfere with this data collection. So I thought of the following solution: what if I were to implement a code that tells Psychopy that when the left mouse button is clicked, it understands it as “Z” and when the right mouse button is clicked, it understands it as “M”? This way, the participant would use a small and lightweight Bluetooth mouse in one hand.”
My attempt:
import {core, event, visual} from ‘psychopy’;
win = new visual.Window([400, 400]);
resp = new psychoJS.eventManager.BuilderKeyResponse();
while (true) {
text = new visual.TextStim(psychoJS.window, {“text”: “Pressione a tecla "z" ou "m"”});
text.draw();
psychoJS.window.flip();
keys = psychoJS.eventManager.getKeys({“keyList”: [“z”, “m”, “escape”, “q”], “timeStamped”: new core.Clock()});
if (keys) {
[key, rt] = keys[0];
if (((key === “z”) || (key === “m”))) {
console.log(Tecla '${key}' pressionada, Tempo de Reação: ${util.pad(Number.parseFloat(rt).toFixed(3), 1)} s
);
break;
} else {
if (((key === “escape”) || (key === “q”))) {
break;
}
}
}
}
psychoJS.window.close();
You don’t need to have the mouse-clicks turn into key-presses, you can just use the mouse-clicks directly. Mouse.getPressed() returns a three-item list with the state of the left, right, and middle mouse buttons (indexes 0, 1, 2 respectively). If the button is pressed, it’s 1. If it’s not, it’s 0. It works more or less the same in Python and JS. psychopy.event - for keypresses and mouse clicks — PsychoPy v2023.2.3
So instead of trying to interpret them as a key-press, you can just record the mouse clicks directly.
“OK, so do I need to create a mouse component in my experiment and assign a code with Mouse.getPressed()?”
Oh, if you’re doing this in builder you shouldn’t need any extra code at all. The Mouse component should store which button they pressed (as well as where they pressed it). Unless you specifically need something to respond to which button they pressed, you should just be able to use the mouse component as-is.
If you want to have this extra boolean tracking which button they pressed, then you should remove all this code from “Begin Routine” and do something like this instead:
In “Each frame”:
clicks = tarefa_resposta.getPressed()
if clicks[2]:
resposta = True
elif clicks[0]:
resposta = False
You’ll need to record that variable somewhere else if you want it stored in your data, or you can just get which button was clicked from the data PsychoPy records anyway and code “True” and “False” after the study.
In my experiment, the location of the click doesn’t matter, the mouse cursor doesn’t even need to appear on the screen. I just want it to understand that clicking the left button means true and clicking the right means false, and that after the click, the next condition in the Excel column of my loop appears.
Do I need to modify any variable of the mouse component?
Almost, but put it in the “each frame” tab instead of the “begin routine” tab. As for the data you probably dont have to modify any settings but i would recommend just doing a test run and seeing if the data contain the info you need.