File: GetKbChar.m

package info (click to toggle)
psychtoolbox-3 3.0.18.12.dfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,576 kB
  • sloc: ansic: 173,181; cpp: 20,885; objc: 5,148; sh: 2,752; python: 1,366; php: 384; makefile: 193; java: 113
file content (118 lines) | stat: -rw-r--r-- 3,248 bytes parent folder | download | duplicates (5)
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
function [ch, when] = GetKbChar(varargin)
% [ch, when] = GetKbChar([deviceIndex][, untilTime=inf][, optional KbCheck arguments...]);
%
% GetKbChar reimplements basic functionality of GetChar() by use of KbCheck
% and KbPressWait. It accepts optionally all arguments that KbCheck accepts
% and passes those arguments to KbCheck, e.g., a keyboard index in order to
% only query a specific keyboard for input.
%
% GetKbChar also returns with empty return values if the optional deadline
% 'untilTime' is reached.
%
% The function only recognizes standard alpha-numeric keys, i.e., letters
% and numbers, and a few special symbols like the ones on top of the
% numeric keys. It only recognizes the delete, space and return keys as
% special function keys, not other keys like Function keys, CTRL, ALT or
% cursor keys. Pressing the ESCape key will also terminate input.
% It always assumes a US keyboard mapping.
%
% It polls the keyboard, so may miss very brief keystrokes and doesn't use
% the keyboard queue.
%
% Use this function if you need a GetChar like interface for simple string
% and number input in situations where GetChar doesn't work reliably, e.g.,
% on some Octave configurations, with Matlab in -nojvm mode or on
% MS-Windows Vista or Windows-7.
%

% History:
% 22.10.2010  mk  Wrote it.
% 14.02.2016  dgp Accept the Escape key, return it as char(27).

persistent shiftkeys;

if isempty(shiftkeys)
    shiftkeys = [KbName('LeftShift'), KbName('RightShift')];
end

% Get keypress, KbCheck style:
when = KbPressWait(varargin{:});
keycode = zeros(1,256);
down = 1;
secs = when;

if length(varargin) < 2
    untilTime = inf;
else
    untilTime = varargin{2};
    if isempty(untilTime)
        untilTime = inf;
    end
end

while down && (secs < untilTime)
    [down, secs, keyincode] = KbCheck(varargin{:});
    if down
        keycode = keycode + keyincode;
        secs = WaitSecs('YieldSecs', 0.001);
    end
end

% Force keycode to 0 or 1:
keycode(keycode > 0) = 1;

% Shift pressed?
if any(keycode(shiftkeys))
    shift = 2;
else
    shift = 1;
end

% Remove shift keys:
keycode(shiftkeys) = 0;

% Translate to ascii style:
ch = KbName(keycode);

% If multiple keys pressed, only use 1st one:
if iscell(ch)
    ch = ch{1};
end

% Decode 1st or 2nd char, depending if shift key was pressed:
if length(ch) == 1
    if shift > 1 && ismember(ch, 'abcdefghijklmnopqrstuvwxyz')
        ch = upper(ch);
    end
elseif length(ch) == 2
    ch = ch(shift);
elseif length(ch) > 2
    if strcmpi(ch, 'Return')
        ch = char(13);
    end
    if strcmpi(ch, 'ENTER')
        ch = char(13);
    end
    if strcmpi(ch, 'space')
        ch = char(32);
    end
    if strcmpi(ch, 'DELETE') || strcmpi(ch, 'BackSpace')
        ch = char(8);
    end
    if strcmpi(ch,'ESCAPE')
       ch=char(27);
    end
    
    % Catch otherwise unhandled special keys:
    if length(ch) > 1
        % Call ourselves recursively, thereby discarding this unmanageable
        % result.
        fprintf('GetKbChar: Warning: Received keypress for key %s. Don''t really know what to do with it.\n', ch);
        fprintf('Maybe you should check for stuck or invalid keys?\n');
        [ch, when] = GetKbChar(varargin{:});
        return;
    end
end

% Done.
return;