[OS9] My working GetWord function!!!

Hi everyone,
After all the trouble I've been through, I've finally got a working GetWord function and I
just waned to share it with the world! Thanks to all those who helped me along the way.

It displays the instructions 'Type a word.' to the screen and allows a participant/user to
enter a word. The charactes entered are displayed on the screen as they are typed.
Hitting Delete will remove the last character. Hitting Return or Enter will end the function.
The function returns the word that was typed, the amount of time that elapsed between
the first time the instruction were displayed and when the first key was pressed, and the
amount of time that elapsed between the first key pressed and when Return was pressed.
It returns an empty char if no word is typed before hitting Return or if a word is typed and
then completely deleted before hitting Return.

It's not very elegant, but it works. I haven't gotten it to crash at least. I'll paste all the
code below. If Yahoo screws it all up (by wrapping my long comment lines) and you'd like
me to email it to you, just let me
know.

Jason
UCLA


%%%%%%%% A function written to allow participants to type in a word and then to have that
word captured along with response times.
%%%%%%%% Written in Matlab OS 9 5.2.1 with last OS9 version of Psychtoolbox (2.55)
%%%%%%%% Will absolutely not work in Mac OSX (as of right now)
%%%%%%%% Will it work in Windows? Beats me!
%%%%%%%% Jason Finley 4/21/05 UCLA
%%%%%%%% jfinley AT ucla DOT edu

function [word, responsetimestart, timetofinish] = GetWordScreenTime(w,xcoord,ycoord);
%w is a pointer to a window, xcoord & ycoord are coordinates in that window where the
word will appear as it is typed.

enterkeynums=[3,13]; %i got these values by testing GetChar by using x=abs(currentchar);
these values are probably arbitrary and specific to Mac OS 9 and GetChar. they are NOT
the same as values returned by KbName.
deletekeynum=8; %same as above
currentkeynum=0;
word='';
responsetimestart=NaN;
begintime=GetSecs;

while (1)
Screen(w,'DrawText','Type a word.',100,100,BlackIndex(w)); %instructions must be
redrawn after each GetChar. There may be a way around this, but I don't know of it.
[currentchar, currentwhen]=GetChar; %here's the GetChar
currentkeynum=abs(currentchar); %this gets a number value for the char typed.
needed because typing 'return' gives what looks like a null char value, or something, so
we've got to convert it to a number in order to use it.

if isempty(word) & isnan(responsetimestart) %if word is empty and responsetimestart
has not been assigned yet, then this must be the first char entered, so responsetimestart
is assigned
Screen('Preference','Tick0Secs',nan); %this line helps to make the response time
more accurate
responsetimestart=currentwhen.secs; %gets seconds for when first key was
pressed
end

if currentkeynum==enterkeynums(1)|currentkeynum==enterkeynums(2) %breaks
when 'return' or 'entered' hit
Screen('Preference','Tick0Secs',nan); %this line helps to make more accurate the
response time
timetofinish=currentwhen.secs; %gets seconds for when return was pressed
break
end

if currentkeynum==deletekeynum %if they hit Delete...
if isempty(word) | size(word,2)==1 %if the word was already empty, or was only
one char long...
word=''; %sets word to empty char
else
word=word(1:size(word,2)-1); %otherwise, removes last char
end
else
word=[word,currentchar]; %if the key pressed was not Delete, add current char
to array
end

Screen(w,'FillRect'); %blanks screen. this is needed so that letters won't overlap if
someone types a character then hits delete and then tyes a different character
Screen(w,'DrawText',word,xcoord,ycoord,BlackIndex(w)); %draws the word to the
screen, as typed so far

end

timetofinish=timetofinish-responsetimestart; %converts so that timetofinish is # of
seconds between first key hit and return key hit
responsetimestart=responsetimestart-begintime; %converts so that responsetimestart is #
of seconds between beginning of this function and first key hit
%%%%%%% END FUNCTION %%%%%%%