首页 > 解决方案 > 等待特定时间单击鼠标右键或左键,然后继续

问题描述

我对以下问题有疑问:我在左耳或右耳随机产生了一些声音,参与者应按下鼠标上的左键或右键(这意味着如果左耳出现声音,则应按下左键) . 我的问题是:参与者只有 3 秒钟的点击时间,然后脚本应该继续。(我有两种方法:一种方法如果我在psychtoolbox中使用GetClicks它将暂停(它不应该暂停),第二种方法如果我在 psychtoolbox 中使用GetMouse我无法定义右或左按钮)。如果有人有任何想法,请告诉我我真的需要它。提前致谢。

%% 1) First way: using from GetClicks causes pause 
clc
clear;
SamplingRate=48000;
F=[250 500 1000 2000 4000];
t=linspace(0,1,SamplingRate);
ind=0;
memory{1,1}={0};
for i=1:length(F)
ind=1+ind;
Frequency = F(i);
y=sin(t*2*pi*Frequency);
sound(y,SamplingRate) ;
%% This here I want to wait 3 second for clicking, after that the script continues but it will stope by using GetClicks
[clicks,~,~,whichButton] = GetClicks; %% This function is related to the Psychtoolbox in Matlab
%%
pause(5)
memory{1,ind} = y';
end
 

%% 2. Second way: using GetMouse is good but I can not define right or left button in this code
clc
clear;
SamplingRate=48000;
wait4resTime = 3;
F=[250 500 1000 2000 4000];
t=linspace(0,1,SamplingRate);
ind=0;
memory{1,1}={0};
Z=[];
for i=1:length(F)
ind=1+ind;
Frequency = F(i);
y=sin(t*2*pi*Frequency);
    tStart=GetSecs;
sound(y,SamplingRate) ;
%% This here I want to wait 3 second for clicking, after that the script continues but I can not define the right or left button 
    MousePress=0; %initializes flag to indicate no response
    while    (MousePress==0 && ( (GetSecs-tStart) < wait4resTime-.1 )) 
        [x,y,buttons]=GetMouse();  %waits for a key-press
     MousePress=any(buttons);  %% I have problem and I can not define right or left button this here.
        Z(1,jnd)=MousePress;      
    end
        elapsedTime = GetSecs-tStart;
%%
pause(4)
memory{1,ind} = y';
end

标签: psychtoolbox

解决方案


可以使用 确定是按下了左键还是按下了右键GetMouse。从返回的按钮数组GetMouse是一个逻辑数组,鼠标上的每个按钮都有 1 个元素。例如:

MousePress = 0;
while(~MousePress)
    [x, y, buttons] = GetMouse();
    MousePress = any(buttons);
end

disp(buttons)

if(buttons(1))
    disp('pressed left button!')
end

if(buttons(2))
    disp('pressed right button')
end

推荐阅读