Function that checks for particular key press?

42 views (last 30 days)
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)

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()

Jeroen van Daatselaar
Jeroen van Daatselaar on 22 Mar 2022
Edited: Jeroen van Daatselaar on 22 Mar 2022
You are probably looking for the KbQueue functions. Try running this code and see if it suits your needs:
If this is what you need, just specify some additional output arguments at KbQueueCheck()
reloop = true;
Kbi = PsychHID('Devices',-1); % device -1 should correspond to the default keyboard
keylist = zeros(256,1); keylist(KbName('ESCAPE')) = 1; % only check whether escape key was pressed
KbQueueCreate(Kbi,keylist);
KbQueueStart(Kbi);
ctr=0;
while reloop
ctr=ctr+1;
fprintf('ctr=%d: test keypress now.\n',ctr);
pause(2);
was_pressed = KbQueueCheck(Kbi);
if was_pressed
fprintf('selected key was pressed during this iteration\n')
end
if ctr == 12
reloop = false;
end
KbQueueFlush(Kbi);
end
KbQueueStop(Kbi)

Community Treasure Hunt

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

Start Hunting!