simulate a keypress in a loop

8 Ansichten (letzte 30 Tage)
HYZ
HYZ am 18 Okt. 2022
Beantwortet: Saffan am 30 Aug. 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 Kommentar
HYZ
HYZ am 18 Okt. 2022
maybe making the psychopy window active between each trial might solve. But I don't know how to make the psychopy window active.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Saffan
Saffan am 30 Aug. 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();

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by