MATLAB Answers

Function that checks for particular key press?

8 views (last 30 days)
Anna
Anna on 14 Aug 2021
Commented: Walter Roberson on 14 Aug 2021
Hi!
I am currently wanting to design a little Psychtoolbox experiment for practice purposes. The experiment involves time estimation.
It starts by showing a red circle in the middle of the screen and one would have to press the space bar after one thinks that 10secs have passed since the circle first appeared. I want to measure the reaction time (i.e. time estimation) and save this data.
Currently I use KbStrokeWait, which waits for ANY keyboard key to be pressed.
% Clear the workspace and the screen
sca;
close all;
clearvars;
% 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, white);
% Get the size of the on screen window
[screenXpixels, screenYpixels] = Screen('WindowSize', window);
% Get the centre coordinate of the window
[xCenter, yCenter] = RectCenter(windowRect);
% Make a base Rect of 200 by 200 pixels
baseRect = [0 0 screenXpixels/4 screenXpixels/4];
% 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 red
rectColor = [1 0 0];
% Draw the rect to the screen
Screen('FillOval', window, rectColor, centeredRect, maxDiameter);
% Flip to the screen
Screen('Flip', window);
% Wait for a key press
KbStrokeWait;
% Clear the screen
sca;
(taken as an example and modified from: Peter Scarfe's demos: https://peterscarfe.com/ovaldemo.html)
However, I want it to only react to the space bar and then go on to the next part of the experiment which would be questions. I also need a function with accurate time measurement to due the kind of my experiment. I have read e.g. KbWait is not so great for timing. Due to lacking expereince I am now unsure which function would be best to use?

Answers (2)

Scott MacKenzie
Scott MacKenzie on 14 Aug 2021
Edited: Scott MacKenzie on 14 Aug 2021
You seem to be using a custom toolbox that I don't have. However, assuming you can use built-in MATLAB functions, the tic and toc functions are typically used for an application as your describe. (It is also possible your custom toolbox has equivalent functions.)
The general approach is to insert tic just before the stimulus appears (to start a clock) and then use toc immediately after the user's response (to stop the clock and record the user's response time). Here's an example that brackets the critical lines in your code:
tic; % start clock
Screen('FillOval', window, rectColor, centeredRect, maxDiameter); % present stimulus
Screen('Flip', window);
KbStrokeWait; % user's response
rt = toc; % stop clock (response time in milliseconds is in rt)
  1 Comment
Walter Roberson
Walter Roberson on 14 Aug 2021
By itself, KbStrokeWait cannot be used to wait for a particular key (the space bar).
It also invokes KbWait, which only checks every 5 ms, and so is often not suitable for reaction timing (where 18 ms or less might be very good and 24 ms or more might be considered a bit slow.)
KbStrokeWait returns a timing that would be more accurate than tic/toc: the difficulty is the internal use of KbWait which polls only every 5ms.

Sign in to comment.


Walter Roberson
Walter Roberson on 14 Aug 2021
Note: KbWait also obeys RestrictKeysForKbCheck, but as you noted, it is not good for timing, since it only checks every 5 ms.
KbStrokeWait() is documented as calling KbWait() so it will obey RestrictKeysForKbCheck as well -- but has the same timing problems as KbWait()

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!