等到用户按下键
Wait until user presses key
我正在做一个项目,我有一个视觉刺激,它由屏幕上沿特定方向移动的许多点组成,在刺激之后,用户需要按下两个键之一('q ' 或 'w').
我想让用户被迫按下按键,即:刺激物(点向特定方向移动)播放固定时间,然后停止(但点继续向随机方向移动)和它一直保持这种状态,直到用户按下“q”或“w”。在用户最终按下一个键后,会有一个听觉反馈(正确或错误的答案)并且刺激再次播放。如此重复直到试验结束。
我的问题是,如何在刺激后不暂停地强制用户按下一个键?因为如果我停下来,所有的点都会“冻结”,而不是在等待答案时向 different/random 方向移动。那么,我如何才能等待“forever/indefinitely”直到用户按下键(‘q’或‘w’)?
我的代码:
% present dots in random directions:
for frame = 1:ceil(maxRT*frameRate) %maxRT is 2 seconds
%calls function related to the drawing of the dots
Screen('DrawingFinished', window1.h);
[flip.waitRT.VBL(block, trial, frame), ...
flip.waitRT.StimOns(block, trial, frame), ...
flip.waitRT.FlipTS(block, trial, frame), ...
flip.waitRT.Missed(block, trial, frame), ...
flip.waitRT.beampos(block, trial, frame)] = Screen('Flip', window1.h, TimingCnt);
TimingCnt = flip.waitRT.VBL(block, trial, frameNum) + window1.frameDur - window1.slack;
[keyIsDown, secs, keyCode] = KbCheck();
keypressed = KbName(keyCode);
if ischar(keypressed) && ~isempty(keypressed) && keyIsDown
%checks which key was pressed, compares pressed key with correct key to check if it's correct or wrong, calculates RT, plays auditory feedback
end
end
http://psychtoolbox.org/docs/KbWait
在 while 循环中使用 KbWait
函数,提供 untilTime
参数作为 10ms。如果 keyCode
有效,则 break 并继续进行。
示例伪代码:
isValid = false;
while ~isValid
% move dots randomly
[secs, keyCode, deltaSecs] = KbWait('untilTime', 0.01);
if (keyCode == 'q') || (keyCode == 'w')
isValid = true;
end
end
% do stuff
我正在做一个项目,我有一个视觉刺激,它由屏幕上沿特定方向移动的许多点组成,在刺激之后,用户需要按下两个键之一('q ' 或 'w').
我想让用户被迫按下按键,即:刺激物(点向特定方向移动)播放固定时间,然后停止(但点继续向随机方向移动)和它一直保持这种状态,直到用户按下“q”或“w”。在用户最终按下一个键后,会有一个听觉反馈(正确或错误的答案)并且刺激再次播放。如此重复直到试验结束。
我的问题是,如何在刺激后不暂停地强制用户按下一个键?因为如果我停下来,所有的点都会“冻结”,而不是在等待答案时向 different/random 方向移动。那么,我如何才能等待“forever/indefinitely”直到用户按下键(‘q’或‘w’)?
我的代码:
% present dots in random directions:
for frame = 1:ceil(maxRT*frameRate) %maxRT is 2 seconds
%calls function related to the drawing of the dots
Screen('DrawingFinished', window1.h);
[flip.waitRT.VBL(block, trial, frame), ...
flip.waitRT.StimOns(block, trial, frame), ...
flip.waitRT.FlipTS(block, trial, frame), ...
flip.waitRT.Missed(block, trial, frame), ...
flip.waitRT.beampos(block, trial, frame)] = Screen('Flip', window1.h, TimingCnt);
TimingCnt = flip.waitRT.VBL(block, trial, frameNum) + window1.frameDur - window1.slack;
[keyIsDown, secs, keyCode] = KbCheck();
keypressed = KbName(keyCode);
if ischar(keypressed) && ~isempty(keypressed) && keyIsDown
%checks which key was pressed, compares pressed key with correct key to check if it's correct or wrong, calculates RT, plays auditory feedback
end
end
http://psychtoolbox.org/docs/KbWait
在 while 循环中使用 KbWait
函数,提供 untilTime
参数作为 10ms。如果 keyCode
有效,则 break 并继续进行。
示例伪代码:
isValid = false;
while ~isValid
% move dots randomly
[secs, keyCode, deltaSecs] = KbWait('untilTime', 0.01);
if (keyCode == 'q') || (keyCode == 'w')
isValid = true;
end
end
% do stuff