# Integrating a touchscreen in a Psychtoolbox script (MATLAB)

**URL:** <https://psychtoolbox.discourse.group/t/integrating-a-touchscreen-in-a-psychtoolbox-script-matlab/4213>\
**Category:** Programming Help\
**Created:** [January 28, 2022, 9:11am UTC](https://psychtoolbox.discourse.group/t/integrating-a-touchscreen-in-a-psychtoolbox-script-matlab/4213 "2022-01-28T09:11:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![WilliamJames](https://avatars.discourse-cdn.com/v4/letter/w/73ab20/32.png) [@WilliamJames](https://psychtoolbox.discourse.group/u/WilliamJames)\
**Post date:** [January 28, 2022, 9:11am UTC](https://psychtoolbox.discourse.group/t/integrating-a-touchscreen-in-a-psychtoolbox-script-matlab/4213/1 "2022-01-28T09:11:47Z")

</div>

have this very simple script in MATLAB. It draws a square in the middle of the screen and waits for the user response, provided by pressing the spacebar. Once given the response, the script presents the square again…and so on.

I want to integrate a touchscreen response, so that the user can respond touching the square on a touchscreen.

I know it possible to do it by using some function in MultiTouchDemo: [Psychtoolbox-3 - MultiTouchDemo](http://psychtoolbox.org/docs/MultiTouchDemo)

But I really cannot integrate it in my script. Any advice?

This is my script:

```auto
try
    screens = Screen('Screens');
    whichscreen = max(screens);
    [myscreen,rect]=Screen('OpenWindow',whichscreen);
    DrawFormattedText(myscreen, 'PRESS ANY KEY TO PROCEED', 'center', 'center');
    Screen('Flip', myscreen);
    KbWait;
    space = KbName('space');
    ntrials = 6;
    colorsequence = [1, 2, 2, 2, 1, 1];
    rt = zeros(ntrials, 1);
    stimulus_duration = zeros(ntrials, 1);
    for i=1:ntrials
        WaitSecs(1);
        Screen('FrameOval', myscreen, 0, CenterRect([0, 0, 10, 10], rect));
        onset_fixation = Screen('Flip', myscreen);
        if colorsequence(i) == 1
            stimuluscolor = [255, 0, 0];
        else
            stimuluscolor = [0, 255, 0];
        end
        Screen('FillRect', myscreen, stimuluscolor, CenterRect([0, 0, 50, 50], ...
            rect));
        random_delay = rand+0.5;
        onset_stimulus = Screen('Flip', myscreen, onset_fixation + random_delay);
        t0 = GetSecs;
        [keyIsDown, secs, keyCode] = KbCheck;
        while keyCode(space)==0 && (secs - onset_stimulus) < 2
            [keyIsDown, secs, keyCode] = KbCheck;
        end
        rt(i) = secs - t0;
        stimulus_offsettime = Screen('Flip', myscreen);
        stimulus_duration(i) = stimulus_offsettime - t0;
    end
    screen('CloseAll');
catch
    Screen('CloseAll')
    rethrow(lasterror)
end

```

Thank you, best regards

---

<div class="post-metadata">

**Author:** ![Ian-Max-Andolina](https://yyz2.discourse-cdn.com/free1/user_avatar/psychtoolbox.discourse.group/ian-max-andolina/32/4_2.png) [@Ian-Max-Andolina](https://psychtoolbox.discourse.group/u/Ian-Max-Andolina)\
**Post date:** [January 28, 2022, 9:23am UTC](https://psychtoolbox.discourse.group/t/integrating-a-touchscreen-in-a-psychtoolbox-script-matlab/4213/2 "2022-01-28T09:23:45Z")

</div>

Have you read the source code for that demo? You are missing all the commands that find, open and retrieve data from the touch device in your code! Read `help TouchInput` to understand what you must do; to summarise that documentation:

> 1. Find the device(s) to use for touch input via GetTouchDeviceIndices().
> 2. Get basic information about a device of choice via GetTouchDeviceInfo().
> 3. Create (TouchQueueCreate()) a touch input queue for the selected device.
> 4. Use TouchQueueStart(), TouchQueueStop() and TouchEventFlush() to start,  
> and stop data collection, or to discard all currently pending touch input.
> 5. Use TouchEventAvail() to find the number of collected touch events, and  
> TouchEventGet() to retrieve events. These events provide all the basic  
> information of interest: The time when the event was received, the (x,y)  
> touch coordinates in different useful coordinate systems, if it was a  
> start of a new touch, the end of an old touch, or some state change on an  
> existing touch, e.g., position, pressure, shape etc. Additional device  
> and operating system specific info (non-portable!) can be retrieved via  
> the helper function GetTouchValuators().
