# How to take response from mouse

**URL:** <https://psychtoolbox.discourse.group/t/how-to-take-response-from-mouse/4391>\
**Category:** Programming Help\
**Tags:** windows, experiment-design\
**Created:** [April 29, 2022, 4:31pm UTC](https://psychtoolbox.discourse.group/t/how-to-take-response-from-mouse/4391 "2022-04-29T16:31:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankit27](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@Ankit27](https://psychtoolbox.discourse.group/u/Ankit27)\
**Post date:** [April 29, 2022, 4:31pm UTC](https://psychtoolbox.discourse.group/t/how-to-take-response-from-mouse/4391/1 "2022-04-29T16:31:39Z")

</div>

Below is my code. I have designed a experiment in MATLAB using psychtoolbox where I am presenting ovals for random duration (50ms and 350 ms). After the ovals are being presented, there is question asked to the participant - Whether the presented oval is short (50ms) or long (350ms)? Subjects need to respond via mouse. If the subject feels that it is **SHORT** they have to click **left mouse button** and if the subject feels **LONG** , then they have to click **right mouse button**. I will be attaching two mouse (one on the left and one on the right sides of computer screen). Right hand dominant person will be responding with the right side mouse and vice versa. Please help me regarding HOW TO COLLECT RESPONSES VIA MOUSE . I am completely out of clue how to do this. I am not getting how to use **GetMouse** or **GetClick** function even after reading the help section. I don’t know how to use any function. If possible please help me out regarding this. I would be highly grateful to you. Thanks in advance.

**CODE:**

% Clear the workspace and the screen  
Screen(‘Preference’, ‘SkipSyncTests’, 1);  
sca;  
close all;  
clear;

%PsychDebugWindowConfiguration;

%%%%%%%%%%Creating Variables %%%%%%

Trial\_Number = (1:20); % Serial Number of trials

% Delay\_time is a variable that will take up random values among (0.05, AND 0.35s- 2 random duration values)  
Delay\_time = zeros(1,20);  
Delay\_time (1:10) = 0.05;  
Delay\_time (11:20) = 0.35;  
Delay\_time = Delay\_time (randperm(20))\*1;

%%%%%%%%%%%%%%%%%%%%%%%% Display Oval on PTB screen %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Here we call some default settings for setting up Psychtoolbox  
PsychDefaultSetup(2);

% Get the screen numbers  
screens = Screen(‘Screens’);

% Draw to the external screen if avaliable  
screenNumber = max(screens);

% Define black and white  
white = WhiteIndex(screenNumber);  
black = BlackIndex(screenNumber);

% Open an on screen window  
[window, windowRect] = PsychImaging(‘OpenWindow’, screenNumber, black);

% Get the size of the on screen window  
[screenXpixels, screenYpixels] = Screen(‘WindowSize’, window);

% Set up alpha-blending for smooth (anti-aliased) lines  
Screen(‘BlendFunction’, window, ‘GL\_SRC\_ALPHA’, ‘GL\_ONE\_MINUS\_SRC\_ALPHA’);

%Set the TextFont & TextSize of the screen window  
Screen(‘TextFont’, window, ‘Ariel’);  
Screen(‘TextSize’, window, 40);

% Get the centre coordinate of the window  
[xCenter, yCenter] = RectCenter(windowRect);

% Here we set the size of the arms of our fixation cross  
fixCrossDimPix = 30;

% Now we set the coordinates (these are all relative to zero we will let  
% the drawing routine center the cross in the center of our monitor for us)  
xCoords = [-fixCrossDimPix fixCrossDimPix 0 0];  
yCoords = [0 0 -fixCrossDimPix fixCrossDimPix];  
allCoords = [xCoords; yCoords];

% Set the line width for our fixation cross  
lineWidthPix = 4;

% Make a base Rect of 200 by 250 pixels  
baseRect = [0 0 200 250];

% For Ovals we set a miximum diameter up to which it is perfect for  
maxDiameter = max(baseRect) \* 1.01;

% Center the rectangle on the centre of the screen  
centeredRect = CenterRectOnPointd(baseRect, xCenter, yCenter);

% Set the color of the rect to gray  
rectColor = [0.5 0.5 0.5];  
penWidthPixels = 6;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% INSTRUCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%

DrawFormattedText(window,[‘Training Phase will begin now.\n\n’…  
‘Press any key to continue.’], ‘center’, ‘center’, white);  
Screen(‘Flip’, window);  
KbWait;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TRIAL STRUCTURE %%%%%%%%%%%%%%%%%%%%%%%%%%%%  
start = GetSecs;  
for i\_trial = 1:20  
% curr = GetSecs; %Gets current time  
% disp(curr-start); %Displays time since  
% start=curr;  
% Draw the fixation cross in white, set it to the center of our screen and % set good quality antialiasing  
Screen(‘DrawLines’, window, allCoords,…  
lineWidthPix, white, [xCenter yCenter], 2);

% Flip to the screen  
Screen(‘Flip’, window);

% Wait for 500 ms  
WaitSecs (0.5);

% Draw the rect to the screen  
Screen(‘FrameOval’, window, rectColor, centeredRect, maxDiameter, penWidthPixels);  
% Flip to the screen  
Screen(‘Flip’, window);  
WaitSecs (Delay\_time (i\_trial)); % random durations for which oval to be presented on screen

% Screen(‘FillOval’, window, [0 0 0], centeredRect); %blank screen  
% Screen(‘Flip’, window);  
% WaitSecs (1); %Wait for 1 sec

DrawFormattedText(window, ‘Short or Long ?’, ‘center’, ‘center’, white); % Question asked to Participants  
Screen(‘Flip’, window)

% Clear the screen  
sca;

---

<div class="post-metadata">

**Author:** ![Ankit27](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@Ankit27](https://psychtoolbox.discourse.group/u/Ankit27)\
**Post date:** [May 2, 2022, 11:41am UTC](https://psychtoolbox.discourse.group/t/how-to-take-response-from-mouse/4391/2 "2022-05-02T11:41:04Z")

</div>

Someone please help me out with this
