How to (1) accept user input while (2) keeping track of the number of character keys being pressed?

I am currently programming a task in which the participant will enter in a string via a prompt, and for this, I am using getEchoString(), a method found the in the Psychtoolbox-3 package. What I would like to do is to also keep track of how many character (alpha-numeric) that are pressed by the user during each getEchoString(). Any advice would be greatly appreciated. Below is the snippet of code which captures the participant's input and checks if it is correct; within, I would like to count the number of keys pressed.
while (sequence_correct == false)
DrawFormattedText(window_pointer, sequence, 'center', 'center', black);
string = GetEchoString(window_pointer, '>', 700, 675, black, [255 255 255]);
if strcmp(string, sequence)
sequence_correct = true;
else
Screen('FillRect', window_pointer); % clear Screen
Screen('DrawText', window_pointer, 'TRY AGAIN', 100, 200, red);
end
end

4 Comments

Daniel - I don't have this toolbox, but can't you just count the number of characters in string using
length(string)
to determine how many characters there are, and so, how many keys were pressed?
I think he's concerned you might do something like hitting the following
c
a
t
backspace
backspace
r
a
t
e
Which, if you just captured the string would read "crate".
I think you can do it by making a customized version of GetEchoString. I don't have the toolbox, but the code looks simple here.
Basically, you can see in this code, the back-spacing is capture by this:
string = string(1:length(string)-1);
Just make a new variable inside call fullstring and then whenever you change string, set fullstring = string except in this one instance. I think the operative changes would be like this:
%outside the while loop create an empty string called fullstring
case 8
% backspace
if ~isempty(string)
% Redraw text string, but with textColor == bgColor, so
% that the old string gets completely erased:
oldTextColor = Screen('TextColor', windowPtr);
Screen('DrawText', windowPtr, output, x, y, bgColor, bgColor);
Screen('TextColor', windowPtr, oldTextColor);
% Remove last character from string:
string = string(1:length(string)-1);
fullstring = [string,'\b\'];
end
otherwise
string = [string, char]; %#ok<AGROW>
fullstring = string;
Then return fullstring instead of string.
Give it a try! Fullstring will now be all of the stuff they entered, and backspaces are represented by \b\ this funny thing, or whatever you want (maybe pick a Unicode character they can't input with their keyboard instead like ™
jgg - I think that your comment would be better suited as an answer given the amount of detail and research that you put in to it! :)

Sign in to comment.

 Accepted Answer

I think you can do it by making a customized version of GetEchoString. I don't have the toolbox, but the code looks simple here.
Basically, you can see in this code, the back-spacing is captured by this:
string = string(1:length(string)-1);
Just make a new variable inside called fullstring and then whenever you change string, set fullstring = string except in this one instance. I think the operative changes would be like this:
%outside the while loop create an empty string called fullstring
case 8
% backspace
if ~isempty(string)
% Redraw text string, but with textColor == bgColor, so
% that the old string gets completely erased:
oldTextColor = Screen('TextColor', windowPtr);
Screen('DrawText', windowPtr, output, x, y, bgColor, bgColor);
Screen('TextColor', windowPtr, oldTextColor);
% Remove last character from string:
string = string(1:length(string)-1);
fullstring = [string,'\b\'];
end
otherwise
string = [string, char]; %#ok<AGROW>
fullstring = string;
Then return fullstring instead of string.
Give it a try! Fullstring will now be all of the stuff they entered, and backspaces are represented by \b\ this funny thing, or whatever you want (maybe pick a Unicode character they can't input with their keyboard instead like ™

More Answers (0)

Community Treasure Hunt

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

Start Hunting!