Azzera filtri
Azzera filtri

simulate a keypress in a loop

5 visualizzazioni (ultimi 30 giorni)
HYZ
HYZ il 18 Ott 2022
Risposto: Saffan il 30 Ago 2023
Hi,
I want to simulate a keypress to trigger a new trial on Psychopy. the first keypress did work to trigger but the subsequent keypress did not work. the code below worked for other windows like notepad etc but not on Psychopy. could anyone suggest? Thanks.
import java.awt.Robot;
import java.awt.event.*;
Sim=Robot;
count = 0;
Sim.mouseMove(0, 0);
pause (3)
Sim.mouseMove(520, 520);
Sim.mousePress(InputEvent.BUTTON2_MASK);
pause (0.5)
Sim.mouseRelease(InputEvent.BUTTON2_MASK);
while count < 5
Sim.keyPress(KeyEvent.VK_ENTER);
count = count + 1;
display (count)
end
  1 Commento
HYZ
HYZ il 18 Ott 2022
maybe making the psychopy window active between each trial might solve. But I don't know how to make the psychopy window active.

Accedi per commentare.

Risposte (1)

Saffan
Saffan il 30 Ago 2023
Hi,
Instead of simulating middle mouse button press, that is, “InputEvent.BUTTON2_MASK”, you can simulate the left mouse button press, that is, “InputEvent.BUTTON1_MASK”. Alternatively, you can activate “psychopy” window in each iteration using the code snippet as follows:
% Get all the active windows
allWindows = java.awt.Window.getWindows();
% Select the desired window
selectedWindow = allWindows(1);
% Set the selected window as active
selectedWindow.toFront();

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by