Psychtoolbox help: How to get inputs with keyboard and present the input on screen
23 views (last 30 days)
Show older comments
Hi there,
I am currently designing an experiment for my phd project and have problems with a spesific task. It is a free recall task so I am asking participants to type in as many words as they can remember in any order (from the list they learned before). So; they have to type in words one by one (presented on the screen) and I have to save them in a variable. When they type in one word and press enter I want it to be cleared from the screen so that they can type in another word.
At the moment the code is only running for one word and the screen is closed right after participants press enter. I want to run it for a certain period of time and close the screen if they cant remember any other word.
Here is the code I have:
%set the colors
white = [255 255 255];
black = [0 0 0];
%Texts
text1 = 'Type in as many words as you can'
%Keyboard code to get what subjects typed
for i = 1:2 %2 here is just to make it run for a certain number (has to be longer)
KbName('UnifyKeyNames'); %used for cross-platform compatibility of keynaming
KbQueueCreate; %creates cue using defaults
KbQueueStart; %starts the cue
returnKey=13; %define return key (used to end)
deleteKey=8; %define delete key (used to delete)
Screen('TextSize',w,36); %sets textsize for instructions
DrawFormattedText(w,text1,'center',screenYpixels * 0.35,white,black); %draws instructions
Screen('Flip',w);
enterpressed=0; %initializes loop flag
AsteriskBuffer=[]; %initializes buffer
while ( enterpressed==0 )
[pressed, firstPress]=KbQueueCheck; %checks for keys
enterpressed=firstPress(returnKey)%press return key to terminate each response
if (pressed && ~enterpressed)
if firstPress(deleteKey) %if delete key then erase last key-press
AsteriskBuffer=AsteriskBuffer(1:end-1); %erase last key-press
else %otherwise add to buffer
firstPress(find(firstPress==0))=NaN;
[endtime Index]=min(firstPress);
AsteriskBuffer=[AsteriskBuffer KbName(Index)]; %adds key to buffer
end
Screen('TextSize',w,36);
DrawFormattedText(w,text1,'center',screenYpixels * 0.35,white,black);
Screen('TextSize',w,40);
[xCenter, yCenter] = DrawFormattedText(w, AsteriskBuffer, 'center','center'); %draws keyspressed
Screen('Flip',w);
end;
WaitSecs(.001);
end;
words_typed(1,i) = string(AsteriskBuffer);
end
% put in small interval to allow other system events
ListenChar(0); %makes it so characters typed do show up in the command window
ShowCursor(); %shows the cursor
Screen('CloseAll'); %Closes Screen
But this code only saves the last word that typed in
Any tips would be appreciated, thank you very much!!!
0 Comments
Answers (1)
Kshittiz
on 21 Sep 2023
Hi Selen,
I understand that you want your code to run for a certain period and save all the words typed during that period. But the code is only saving the last word entered.
Upon further investigation of the code, I noticed that the issue is being encountered due to incorrect placement of the following piece of code:
words_typed(1,i) = string(AsteriskBuffer)
It is currently placed outside the while loop, which is why only the last word is being saved. Moving this piece of code inside the while loop after modifying it will help you fix the issue.
To save all the words which are being typed, you can initialize the ‘words_typed’ array in the beginning, and replace the piece of code with the following code:
words_typed(end+1) = string(AsteriskBuffer);
This will make sure that all the words which are being entered would be saved.
I hope this will help you fix the issue.
Thanks and Regards,
Kshittiz
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!