how to get psychtoolbox to wait for keypress but move on if it hasn't recieved one in a set time

24 次查看(过去 30 天)
i'm trying to code an experiment, where a participant must make an orientation judgement (more clockwise or more anticlockwise with the keyboard). i want to code it so that if they don't make a response within, say 2 seconds the code continues.
i have tried things like while WaitSecs(2) < 2; [secs, keyCode, deltaSecs] = KbWait(); end
But this doesn't save the variables for use outside of the while loop. Thanks in advance!

采纳的回答

Mihaela Duta
Mihaela Duta 2016-11-5
编辑:Mihaela Duta 2016-11-5
There are some missing details in the specification given in the question, so I am going to make the following assumptions:
  • Item one only one key-press is expected - let's say a press on the left or right arrow key
  • Item two immediately after the response is given the waiting for response stops - the time taken to make the response is the reaction time
The follwing code should implement your requirements according to the above assumptions
% improve portability of your code acorss operating systems
KbName('UnifyKeyNames');
% specify key names of interest in the study
activeKeys = [KbName('LeftArrow') KbName('RightArrow')];
% set value for maximum time to wait for response (in seconds)
t2wait = 2;
% if the wait for presses is in a loop,
% then the following two commands should come before the loop starts
% restrict the keys for keyboard input to the keys we want
RestrictKeysForKbCheck(activeKeys);
% suppress echo to the command line for keypresses
ListenChar(2);
% get the time stamp at the start of waiting for key input
% so we can evaluate timeout and reaction time
% tStart can also be the timestamp for the onset of the stimuli,
% for example the VBLTimestamp returned by the 'Flip'
tStart = GetSecs;
% repeat until a valid key is pressed or we time out
timedout = false;
% initialise fields for rsp variable
% that would contain details about the response if given
rsp.RT = NaN; rsp.keyCode = []; rsp.keyName = [];
while ~timedout,
% check if a key is pressed
% only keys specified in activeKeys are considered valid
[ keyIsDown, keyTime, keyCode ] = KbCheck;
if(keyIsDown), break; end
if( (keyTime - tStart) > t2wait), timedout = true; end
end
% store code for key pressed and reaction time
if(~timedout)
rsp.RT = keyTime - tStart;
rsp.keyCode = keyCode;
rsp.keyName = KbName(rsp.keyCode);
end
% if the wait for presses is in a loop,
% then the following two commands should come after the loop finishes
% reset the keyboard input checking for all keys
RestrictKeysForKbCheck;
% re-enable echo to the command line for key presses
% if code crashes before reaching this point
% CTRL-C will reenable keyboard input
ListenChar(1)
  6 个评论
Edan Daniel
Edan Daniel 2023-5-18
编辑:Edan Daniel 2023-5-18
It's the initialization of the reaction time field (RT) in the response (rsp) structure. That is, setting that until a reaction time is recorded, rsp.RT equals nothing (NaN). If a key press is detected, this field should be updated as follows (as seen a few lines below the line in question in Mihaela's example)
rsp.RT = keyTime - tStart;
That is, replacing the NaN with the recorded reaction time: the difference between the start time, and the time of key press. If no key press is detected, this field will not be updated, and will keep holding a 'NaN' indicating no reaction time was recorded.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Timing and presenting 2D and 3D stimuli 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by