在 Psychtoolbox 中获得鼠标响应时如何修复 while 循环错误?

问题描述

我想通过获得鼠标响应来获得 MATLAB 中 PsychtoolBox 参与者的评分;但是,当我这样做时,while 循环不允许我获得所有回复,而且我只能获得 1 分而不是 4 分(或 4-5 分而不是 16 分)。据我所知,用于获得鼠标响应的 while 循环仅从单词列表中获得一个评分,然后关闭并假设我的答案适用于所有单词。我无法解决问题,因为老实说我不明白原因。该代码键盘响应完美配合(我可以从 4 个形容词 x 4 个单词中获得 16 个评分)。因此,我相信我在 while 循环中遗漏了一些东西。

这是我的代码的一部分:

% 获取鼠标值 SetMouse(round(rand * screenXpixels),round(rand * screenYpixels),window);

for i=1:Shuffle(1:length(words)) 对于 z=1:Shuffle(1:length(adjectives))

    while true    

    ...%There is written the codes to display adjectives and words

    [x,~,buttons] = GetMouse(Window); 
    y=centerY; 
                
                x = x+1;
                x = min(x,screenXpixels);
                mc_pick = ceil(x/gridX);
                choice = 430-98*mc_pick; %choice 1-7

                % Do not let mouse to exceed rectangle
                if x < RectangleDimensions(1)+20 % if x coordinate of the mouse gets smaller than left border's x coordinate of green Box
                    x=RectangleDimensions(1)+20; % keep it at left border's x coordinate
                elseif x > RectangleDimensions(3)-20 % if x coordinate of the mouse exceeds right border's x coordinate of green Box
                    x=RectangleDimensions(3)-20; % keep it at right border's x coordinate
                end

                %Draw the vertical red Box adjusted to the mouse
                %Its position changes according to mouse movements. It starts on the center since the mouse is set at the center. 
                %The Y axis for the red rectangle should not change according to the mouse
                %movements. It must stay at the center.
                Screen('FillRect',Window,red,[centerX-choice-centerX/51 3/2*centerY-30 centerX-choice+centerX/8 3/2*centerY+30],4);
    
                Screen('Flip',Window);

              
                if  any(buttons)
                    break
                end
                
               
    end

        ...
        

结束 结束

解决方法

我认为问题在于您正在检查鼠标何时被按下,但您并没有等到鼠标按钮被释放。在这种情况下,当一个人按下一个按钮时,它可能会在释放鼠标按钮之前循环多次尝试。

这是正在发生的事情的简化示例:

% checking for a mouse press without checking for the release
% pressing a button will cycle through many trials before the button is released
disp('Press the Mouse Button');
for trial = 1:10
    fprintf('On Trial %i\n',trial);
    buttons = 0;
    while ~any(buttons)
        [~,~,buttons]=GetMouse;
        WaitSecs(0.001);
    end
end

相反,请等到释放鼠标按钮,然后再继续下一个试验:

% checking for a mouse press and then check for the release
disp('Press the Mouse Button');
for trial = 1:10
    fprintf('On Trial %i\n',buttons]=GetMouse;
        WaitSecs(0.001);
    end
    
    % wait until all buttons released
    while any(buttons)
        [~,buttons]=GetMouse;
        WaitSecs(0.001);
    end
end

在原始代码示例的上下文中,更改:

            if  any(buttons)
                break
            end

致:

if any(buttons)
    while any(buttons)
        [~,buttons]=GetMouse;
    end
    break
end

这将等到鼠标按钮被释放,然后才退出 while 循环。