-
Notifications
You must be signed in to change notification settings - Fork 0
/
getkey.txt
133 lines (124 loc) · 3.83 KB
/
getkey.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
function [ch, tim] = getkey(N,nonascii)
% GETKEY - get a keypress
% CH = GETKEY waits for a single keypress and returns the ASCII code. It
% accepts all ascii characters, including backspace (8), space (32),
% enter (13), etc, that can be typed on the keyboard.
% Non-ascii keys (ctrl, alt, ..) return a NaN. CH is a double.
%
% CH = GETKEY(N) waits for N keypresses and returns their ASCII codes.
% GETKEY(1) is the same as GETKEY without arguments.
%
% GETKEY('non-ascii') or GETKEY(N,'non-ascii') uses non-documented
% matlab features to return a string describing the key pressed.
% In this way keys like ctrl, alt, tab etc. can also distinguished.
% The return is a string (when N = 1) or a cell array of strings.
%
% [CH,T] = GETKEY(...) also returns the time between the start of the
% function and each keypress. This is, however, not that accurate.
%
% This function is kind of a workaround for getch in C. It uses a modal,
% but non-visible window, which does show up in the taskbar.
% C-language keywords: KBHIT, KEYPRESS, GETKEY, GETCH
%
% Examples:
%
% fprintf('\nPress any key: ') ;
% ch = getkey ;
% fprintf('%c\n',ch) ;
%
% fprintf('\nPress the Ctrl-key within 3 presses: ') ;
% ch = getkey(3,'non-ascii')
% if ismemmber('control', ch),
% fprintf('OK\n') ;
% else
% fprintf(' ... wrong keys ...\n') ;
% end
%
% See also INPUT, UIWAIT
% GETKEYWAIT (File Exchange)
% for Matlab 6.5 and higher
% version 2.0 (jun 2012)
% author : Jos van der Geest
% email : [email protected]
%
% History
% 1.0 2005 - creation
% 1.1 dec 2006 - modified lay-out and help
% 1.2 apr 2009 - tested for more recent MatLab releases
% 1.3 jan 2012 - modified a few properties, included check is figure still
% exists (after comment on FEX by Andrew).
% 2.0 jun 2012 - added functionality to accept multiple key presses
t00 = tic ; % start time of this function
% check the input arguments
error(nargchk(0,2,nargin))
switch nargin
case 0
nonascii = '' ;
N = 1 ;
case 1
if ischar(N),
nonascii = N ;
N = 1 ;
else
nonascii = '' ;
end
end
if numel(N) ~= 1 || ~isnumeric(N) || N < 1 || fix(N) ~= N
error('N should be a positive integer scalar.') ;
end
% Determine the callback string to use
if strcmpi(nonascii,'non-ascii'),
% non-ascii characters are accepted
nonascii = true ;
callstr = 'set(gcbf,''Userdata'',get(gcbf,''Currentkey'')) ; uiresume ' ;
elseif isempty(nonascii)
nonascii = false ;
% only standard ascii characters are accepted
callstr = 'set(gcbf,''Userdata'',double(get(gcbf,''Currentcharacter''))) ; uiresume ' ;
else
error('String argument should be the string ''non-ascii''') ;
end
% Set up the figure
% May be the position property should be individually tweaked to avoid visibility
fh = figure(...
'name','Press a key', ...
'keypressfcn',callstr, ...
'windowstyle','modal',...
'numbertitle','off', ...
'position',[0 0 1 1],...
'userdata','timeout') ;
try
ch = cell(1,N) ;
tim = zeros(1,N) ;
% loop to get N keypresses
for k=1:N
% Wait for something to happen, usually a key press so uiresume is
% executed
uiwait ;
tim(k) = toc(t00) ; % get the time of the key press
ch{k} = get(fh,'Userdata') ; % and the key itself
if isempty(ch{k}),
if nonascii
ch{k} = NaN ;
else
ch{k} = '' ;
end
end
end
if ~nonascii
ch = [ch{:}] ;
else
if N==1
ch = ch{1} ; % return as a string
end
% return as a cell array of strings
end
catch
% Something went wrong, return empty matrices.
ch = [] ;
tim = [] ;
end
% clean up the figure, if it still exists
if ishandle(fh)
delete(fh) ;
end