Waiting for keypresses to continue

11 次查看(过去 30 天)
Hello everyone,
I'm writing a script in which one or two subjects have to press a key (alone or toghether) to switch on a lightbulb. To record they keypresses I'm using the function Kbqueuecheck. However, using this function I am able to say the script to wait x seconds (with WiatSecs(x)), but I am not able to say the script to wait untile one or both participats press their keys, and after that contiuing the script. I have tried with a while loop, as in the following example, but with no success. Have you any suggestion?
AKey=KbName('A'); %accensione della luce soggetto 1
LKey=KbName('L'); %accensione della luce soggetto 1
keyFlags = zeros(256, 1);
% Set to 1 the indexed of the keys we are interested in
keyFlags([AKey, LKey]) = 1;
% Create two separate vectors for the answers, one for each
%searching for keypress
timedout = false;
pressed = 0;
%while pressed < 1;
TrialOrder = Shuffle(1 : size(TrialsTable,1));
Trial = 0;
for i= TrialOrder
while ~timedout && ~pressed
% Draw a black rectangle that cover all the screen (background)
Screen('FillRect', windowPtr, [0 0 0], windowRect);
% In each cycle, import the images to be used for the current trial and convert it to a textur
IMGcue=imread(TrialsTable.OFF{i}); % In case you want to get numbers, use () instead of {}
IMGcueResized = imresize(IMGcue,[windowRect(4) windowRect(3)]);
IMGtexturecueResized=Screen('MakeTexture', windowPtr, IMGcueResized);
Screen('DrawTexture', windowPtr, IMGtexturecueResized);
% Flip-> display the stimulus: record the moment when the stimulus
% appear, it's needed to compute the RTs
StimulusSecs=Screen('Flip', windowPtr);
% Create and start the queue
KbQueueCreate(devs(1), keyFlags); % Generating the queue
KbQueueStart(devs(1)); % Initialising the queue
% Close queue and Collect responses
[pressed, firstPress, firstRelease, lastPress, lastRelease]=KbQueueCheck(devs(1));
KbQueueStop(devs(1));
% check keyboard
if pressed == 1
timedout = true;
WaitSecs(TrialsTable.DELAY(i)/1000);
%In each cycle import the images to be used for the current trial and convert it to a texture
IMGoutput=imread(TrialsTable.ON{i});
IMGoutputResized = imresize(IMGoutput,[windowRect(4) windowRect(3)]);
IMGtextureoutputResized=Screen('MakeTexture', windowPtr, IMGoutputResized);
Screen('DrawTexture', windowPtr, IMGtextureoutputResized);
% Flip-> display the stimulus: record the moment when the
% stimulus appear, it's needed to compute the RTs
OutputSecs=Screen('Flip', windowPtr);
end
end
end

回答(1 个)

T.Nikhil kumar
T.Nikhil kumar 2023-10-26
Hello Marika,
As per my understanding you want to continue with the execution of your code after you detect one or more key presses on the device after creating and starting the queue of events.
I would suggest you to use the ‘pause’ function for this purpose. This function temporarily stops execution and waits for the user to press any key. Introduce the ‘pause’ function without any arguments after starting your queue.
Alternatively, you can try using the ‘KbQueueWait’ function, in the ‘Psych Toolbox’ itself, that waits for a key to be pressed and returns the time of the press.
Refer to the following documentation to understand more about ‘pause’ function.
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by