GETKEY - get a single keypress CH = GETKEY waits for a keypress and returns the ASCII code. 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('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. CH is a string. 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: ') ; if strcmp(getkey('non-ascii'),'control'), fprintf('OK\n') ; else fprintf(' ... wrong key ...\n') ; end See also INPUT, UIWAIT GETKEYWAIT (File Exchange)
0001 function ch = getkey(m) 0002 0003 % GETKEY - get a single keypress 0004 % CH = GETKEY waits for a keypress and returns the ASCII code. Accepts 0005 % all ascii characters, including backspace (8), space (32), enter (13), 0006 % etc, that can be typed on the keyboard. Non-ascii keys (ctrl, alt, ..) 0007 % return a NaN. CH is a double. 0008 % 0009 % CH = GETKEY('non-ascii') uses non-documented matlab features to return 0010 % a string describing the key pressed. In this way keys like ctrl, alt, 0011 % tab etc. can also distinguished. CH is a string. 0012 % 0013 % This function is kind of a workaround for getch in C. It uses a modal, 0014 % but non-visible window, which does show up in the taskbar. 0015 % C-language keywords: KBHIT, KEYPRESS, GETKEY, GETCH 0016 % 0017 % Examples: 0018 % 0019 % fprintf('\nPress any key: ') ; 0020 % ch = getkey ; 0021 % fprintf('%c\n',ch) ; 0022 % 0023 % fprintf('\nPress the Ctrl-key: ') ; 0024 % if strcmp(getkey('non-ascii'),'control'), 0025 % fprintf('OK\n') ; 0026 % else 0027 % fprintf(' ... wrong key ...\n') ; 0028 % end 0029 % 0030 % See also INPUT, UIWAIT 0031 % GETKEYWAIT (File Exchange) 0032 0033 % for Matlab 6.5 and higher 0034 % version 1.3 (jan 2012) 0035 % author : Jos van der Geest 0036 % email : jos@jasen.nl 0037 % 0038 % History 0039 % 2005 - creation 0040 % dec 2006 - modified lay-out and help 0041 % apr 2009 - tested for more recent MatLab releases 0042 % jan 2012 - modified a few properties, included check is figure still 0043 % exists (after comment on FEX by Andrew). 0044 0045 % Determine the callback string to use 0046 if nargin == 1, 0047 if strcmpi(m,'non-ascii'), 0048 callstr = 'set(gcbf,''Userdata'',get(gcbf,''Currentkey'')) ; uiresume ' ; 0049 else 0050 error('Argument should be the string ''non-ascii''') ; 0051 end 0052 else 0053 callstr = 'set(gcbf,''Userdata'',double(get(gcbf,''Currentcharacter''))) ; uiresume ' ; 0054 end 0055 0056 % Set up the figure 0057 % May be the position property should be individually tweaked to avoid visibility 0058 fh = figure(... 0059 'name','Press a key', ... 0060 'keypressfcn',callstr, ... 0061 'windowstyle','modal',... 0062 'numbertitle','off', ... 0063 'position',[0 0 1 1],... 0064 'userdata','timeout') ; 0065 try 0066 % Wait for something to happen 0067 uiwait ; 0068 ch = get(fh,'Userdata') ; 0069 if isempty(ch), 0070 ch = NaN ; 0071 end 0072 catch 0073 % Something went wrong, return and empty matrix. 0074 ch = [] ; 0075 end 0076 0077 if ishandle(fh) 0078 delete(fh) ; 0079 end