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 Center および File ExchangeInstallation and Operational Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by