Borrar filtros
Borrar filtros

how to get psychtoolbox to wait for keypress but move on if it hasn't received one and end the code if it has

8 visualizaciones (últimos 30 días)
Hi, I'm trying to get psychtoolbox to wait for keypress (left or right arrow) and end the program or wait for keypress (space bar) and move to the next line of code or wait for 5 seconds and if no key is pressed it moves to the next line of code. This is what I have so far:
activeKeys = [KbName('LeftArrow') KbName('RightArrow') KbName('space')];
RestrictKeysForKbCheck(activeKeys);
keysforcheck= [KbName('LeftArrow') KbName('RightArrow')];
keysforcheck2= [KbName('space')];
Screen('FrameRect', window, allColors, centeredRect);
Screen('DrawDots', window, d, dotsize, dotscolor, [], 2);
Screen('FillRect', window, colour, centeredRect);
Screen('DrawDots', window, a, dotsize, colour, [], 4);
while (keyIsDown)
if KbName(find(keyCode))==keysforcheck
sca
elseif KbName(find(keyCode))==keysforcheck2
Screen('FrameRect', window, allColors, centeredRect);
Screen('DrawDots', window, b, dotsize, dotscolor, [], 2);
Screen('FillRect', window, colour, centeredRect);
Screen('DrawDots', window, a, dotsize, colour, [], 4);
elseif WaitSecs(5)>5
Screen('FrameRect', window, allColors, centeredRect);
Screen('DrawDots', window, b, dotsize, dotscolor, [], 2);
Screen('FillRect', window, colour, centeredRect1);
Screen('DrawDots', window, a, dotsize, colour, [], 4);
end
end
Screen('Flip', window);
Screen('CloseAll');

Respuestas (1)

Riya
Riya el 26 de Dic. de 2023
Hello Julie,
As per my understanding, you want to know how to get psych toolbox to wait for keypress.
Please note that to achieve the desired behaviour, you need to utilize a loop that checks for key presses and keeps track of the time to handle the 5-second timeout. Here's a modified version of your code that should work for your requirements:
% Assuming 'window' and other relevant variables are already defined
% Define active keys
activeKeys = [KbName('LeftArrow') KbName('RightArrow') KbName('space')];
RestrictKeysForKbCheck(activeKeys);
% Start the loop at the current time
startTime = GetSecs;
timeOut = 5; % seconds
% Display initial screen
Screen('FrameRect', window, allColors, centeredRect);
Screen('DrawDots', window, d, dotsize, dotscolor, [], 2);
Screen('FillRect', window, colour, centeredRect);
Screen('DrawDots', window, a, dotsize, colour, [], 4);
Screen('Flip', window);
% Wait for a key press or for time out
while 1
% Check for key press
[keyIsDown, secs, keyCode] = KbCheck;
% If a key was pressed, determine which key
if keyIsDown
if keyCode(KbName('LeftArrow')) || keyCode(KbName('RightArrow'))
% If left or right arrow was pressed, end the program
sca;
break;
elseif keyCode(KbName('space'))
% If space bar was pressed, move to the next line of code
Screen('FrameRect', window, allColors, centeredRect);
Screen('DrawDots', window, b, dotsize, dotscolor, [], 2);
Screen('FillRect', window, colour, centeredRect);
Screen('DrawDots', window, a, dotsize, colour, [], 4);
Screen('Flip', window);
break;
end
while KbCheck; end % Wait until all keys are released.
end
% Check for time out
if GetSecs - startTime > timeOut
% If 5 seconds have passed without a key press, move to the next line of code
Screen('FrameRect', window, allColors, centeredRect);
Screen('DrawDots', window, b, dotsize, dotscolor, [], 2);
Screen('FillRect', window, colour, centeredRect1);
Screen('DrawDots', window, a, dotsize, colour, [], 4);
Screen('Flip', window);
break;
end
end
% Continue with rest of the code...
% Screen('CloseAll'); % Uncomment this if you want to close the screen at the end of the program
Remember to replace `window`, `allColors`, `centeredRect`, `d`, `dotsize`, `dotscolor`, `colour`, `a`, `b`, and `centeredRect1` with the actual variables you have defined in your code.
This code snippet checks for a key press and exits the loop if the left or right arrow keys are pressed, ending the program with `sca`. If the space bar is pressed, the loop is exited and the next part of the code is executed. If no key is pressed within 5 seconds, the loop exits and the code continues. Note that `Screen('CloseAll')` will close all windows, so you should use it only when you want to end the program completely.
For more information you can refer following articles for more information:
I hope it helps!

Categorías

Más información sobre Timing and presenting 2D and 3D stimuli en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by