How to (1) accept user input while (2) keeping track of the number of character keys being pressed?
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
Geoff Hayes
2016-2-22
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! :)
采纳的回答
jgg
2016-2-22
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 ™
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!