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

조회 수: 2 (최근 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
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
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개)

카테고리

Help CenterFile Exchange에서 Installation and Operational Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by