Code covered by the BSD License  

Highlights from
GETK

image thumbnail

GETK

by

 

13 Jul 2012 (Updated )

Wait for and identify a pressed key

getk(N,pos)
function [ch, tim] = getk(N,pos)
% GETK - wait for a keypress
%
% The function GETK fulfills the similar tasks as its prototype, the
% function GETKEY. It waits for a keyboard keypress, stores the key
% identifier and a period of the waiting. Since the new function behaves
% differently from its origin, it has got another name. Appart from GETKEY,
% the function GETK comprises names of ascii characters and non-ascii keys
% into a single string. The function creates a modal figure window with 
% a name 'PRESS A KEY' at a required place of the screen.
%
%   CH = GETK waits for a single keypress. It accepts all ascii characters,
%   including backspace '\', space ' ', etc, and non-ascii keys (ctrl, alt,
%   return, ...) that can be typed on the keyboard,  Each ascii key
%   generates a single character. The other keys generate strings of their
%   names. The figure window appears at a current position of the mouse
%   cursor.  
%
%   CH = GETK(N) waits for N keypresses and returns their ASCII codes or
%   names separated by spaces. GETK(1) is the same as GETK without
%   arguments.
%
%   CH = GETK(N,pos) accepts also a user-defined position of the modal
%   figure window. The variable pos is a string
%   'on'    for a position of the figure window that appears at a current
%           position of the mouse cursor,
%   'off'   for hidden figure window, and
%   '[x,y]' for the figure window placed at normalized coordinates of the
%           screen.
%
%   [CH,T] = GETK(...) returns also the time T elapsed between the start of
%   the function and each keypress. This is, however, less accurate.
%
% Examples:
%
%    ch = getk;
%    fprintf('%c\n',ch);
% % ----------------------------------------------
%    fprintf('\nPress three keys with Ctrl-key among them:');
%    ch = getk(3)               %   '5 Ctrl Enter'
%    if ismember('control', ch),
%      fprintf('OK\n');         %   Output OK
%    else
%      fprintf(' ... wrong keys ...\n') ;
%    end
% % ----------------------------------------------
%    fprintf('\nPress the keys  F12 Ctrl 1');
%    ch = getk(4)               %   outputs the string:  'f12 control 1'  
% % ----------------------------------------------
%    fprintf('\nPress any key:');
%    ch = getk(1,'off');        %   waits for a key without figure window 
% % ----------------------------------------------
%    ch = getk(2,'[.3 .2]')     %   waits for 2 keypresses with figure
%                               %   window at the given position on screen
%
%  See also INPUT, UIWAIT, GETKEY,
%           GETKEYWAIT (File Exchange)

% for Matlab 6.5 and higher
% author : Miroslav Balda
% e-mail : miroslav@balda.cz

% History
% 2012-07-10, v.1.0 created by a modification of the function GETKEY v.2.0,
%                   FEX File ID #7465 of Jos van der Geest

t00 = tic ;                     % starting time of the function GETK

error(nargchk(0,2,nargin))      % check the input arguments

sz  = get(0,'ScreenSize');
pl  = get(0,'PointerLocation');
pl  = [pl./sz(3:4) .105 .0001];  %   normalized pointer location
if nargin < 2
    pos = [];
    if nargin == 0
        N = 1;
    elseif numel(N) ~= 1 || ~isnumeric(N) || N < 1 || fix(N) ~= N
        error('N should be a positive integer scalar.');
    end
else
    switch pos
        case 'on'
            pos = [];
        case 'off'
            pos = [0 1 .1 .1];
        otherwise
            pos = [eval(pos) .105 .0001];
            if isreal(pos) 
                if length(pos)~=4
                    error('pos should be vector ''[x, y]''');
                end
            else
                error(['unrecognized pos:' sprintf('\n%s',pos)])
            end
    end
end

if isempty(pos), pos = pl; end

% Determine the callback string to use
callstr = 'set(gcbf,''Userdata'',get(gcbf,''Currentkey'')) ; uiresume ' ;

% Set up the figure
fh = figure(...
    'name','PRESS A KEY', ...
    'keypressfcn', callstr, ...
    'windowstyle','modal',...
    'numbertitle','off', ...
    'position',  pos,...
    'userdata','timeout');

ch  = cell(1,N);
tim = zeros(1,N);

for k = 1:N                             % loop to get N keypresses
    uiwait;                             % Wait for a key press
    tim(k) = toc(t00);                  % get the time of the key press
    ch{k}  = [get(fh,'Userdata'),' '];  % and the key itself
end
ch  = [ch{:}];
ch  = ch(1:end-1);

% clean up the figure, if it still exists
if ishandle(fh), delete(fh); end

Contact us