function result = ButtonName(numName);

% function result = ButtonName(numName);
%
% ButtonName maps between PST hand puck button names and their numbers in a 
% style analogous to KbName.
%
% Key names are the strings:
%      
%      'left thumb', 'right thumb',  
%      'left index, 'right index',
%      'left middle', 'right middle',
%      'left ring', 'right ring',
%      'left pinky', 'right pinky'
%
%   ButtonName is commonly used like this:
%
%   ...
%   stopKeys = [ButtonName('left thumb') ButtonName('right thumb')];
%   press = GetButtons
%   stop = any(press(stopKeys);
%   if stop
%		fprintf(1,'The subject wants to stop !\n');
%   end %if
%   ...
%
%   
% See Also: KbName, InitializeFMRI, WaitScanner, GetButtons, ButtonName.
%
%  history
%
%  2/2/00 awi wrote it.   
%

hands = {'left', 'right'};
fingers = {'thumb', 'index', 'middle', 'ring', 'pinky'};



% case of nothing is passed
if (nargin == 0)  %wait for button press then give name%
	numName = 0;
	while ~any(numName)
		numName = GetButtons;
	end %while
	flushevents('keyDown');
	numName = find(numName);
end %if


%case of nothing passed, case of keycode passed
if isa(numName, 'double')
	if (length(numName) == 10)
		numName = find(numName);
	end 
	if (numName <= 5)
		result = 'right ';
	else
		result = 'left ';
	end %else
	result = [result fingers{mod(numName-1,5) + 1}];
end %if

%case of a key name is passed
if ischar(numName)
	numName = lower(numName);
	if strncmp('left', numName(1:4), 4)
		result = 5;
	elseif strncmp('right', numName(1:5), 5)
		result = 0;
	else
		error ('invalid button name passed to function ButtonName');
	end %else
	for i = 1:5
		theLen = length(numName);
		cmpLen = length(fingers{i});
		if strcmp(numName((theLen - cmpLen) + 1:theLen), fingers{i});
			result = result + i;
		end %if
	end %for
end %if


		
	

		
	
	
