|
1 | | -function responseEvents = getResponse(action, cfg, expParameters, getOnlyPress) |
| 1 | +function responseEvents = getResponse(action, deviceNumber, cfg, getOnlyPress) |
2 | 2 | % wrapper function to use KbQueue |
3 | 3 | % The queue will be listening to key presses on the response box as defined |
4 | 4 | % in the cfg structure : see setParameters for more details. |
|
43 | 43 | % pressed == 0 --> the key was released |
44 | 44 |
|
45 | 45 |
|
| 46 | +if nargin < 2 || isempty(deviceNumber) |
| 47 | + deviceNumber = -1; |
| 48 | +end |
| 49 | + |
| 50 | +if nargin < 3 |
| 51 | + cfg = struct(... |
| 52 | + 'keyboard', struct('responseKey', {})... |
| 53 | + ); |
| 54 | +end |
| 55 | + |
46 | 56 | if nargin < 4 |
47 | 57 | getOnlyPress = true; |
48 | 58 | end |
49 | 59 |
|
50 | 60 | responseEvents = struct; |
51 | 61 |
|
52 | | -responseBox = cfg.responseBox; |
53 | | - |
54 | 62 | switch action |
55 | 63 |
|
56 | 64 | case 'init' |
|
60 | 68 | ListenChar(-1); |
61 | 69 |
|
62 | 70 | % Clean and realease any queue that might be opened |
63 | | - KbQueueRelease(responseBox); |
| 71 | + KbQueueRelease(deviceNumber); |
64 | 72 |
|
65 | | - keysOfInterest = setKeysOfInterest(expParameters); |
| 73 | + keysOfInterest = setKeysOfInterest(cfg.keyboard); |
66 | 74 |
|
67 | 75 | % Create the keyboard queue to collect responses. |
68 | | - KbQueueCreate(responseBox, keysOfInterest); |
69 | | - |
70 | | - |
71 | | - case 'start' |
72 | | - |
73 | | - KbQueueStart(responseBox); |
| 76 | + KbQueueCreate(deviceNumber, keysOfInterest); |
74 | 77 |
|
| 78 | + case 'start' |
| 79 | + KbQueueStart(deviceNumber); |
75 | 80 |
|
76 | 81 | case 'check' |
| 82 | + responseEvents = getAllKeyEvents(responseEvents, deviceNumber, getOnlyPress); |
77 | 83 |
|
78 | 84 | responseEvents = getAllKeyEvents(responseEvents, responseBox, getOnlyPress); |
79 | 85 |
|
80 | 86 | checkAbort(cfg) |
81 | 87 |
|
82 | | - case 'flush' |
83 | | - |
84 | | - KbQueueFlush(responseBox); |
85 | | - |
86 | | - |
87 | | - case 'stop' |
| 88 | + case 'flush' |
| 89 | + KbQueueFlush(deviceNumber); |
| 90 | + |
| 91 | + case 'stop' |
| 92 | + KbQueueStop(deviceNumber) |
88 | 93 |
|
89 | | - KbQueueRelease(responseBox); |
| 94 | + case 'release' |
| 95 | + KbQueueRelease(deviceNumber); |
90 | 96 |
|
91 | 97 | % Give me my keyboard back... Pretty please. |
92 | 98 | ListenChar(0); |
93 | | - |
94 | | - |
| 99 | + |
95 | 100 | end |
96 | 101 |
|
97 | | -talkToMe(action, expParameters); |
| 102 | +talkToMe(action); |
98 | 103 |
|
99 | 104 | end |
100 | 105 |
|
101 | 106 |
|
102 | | -function keysOfInterest = setKeysOfInterest(expParameters) |
| 107 | +function keysOfInterest = setKeysOfInterest(keyboard) |
103 | 108 | % list all the response keys we want KbQueue to listen to |
104 | 109 | % by default we listen to all keys |
105 | 110 | % but if responseKey is set in the parameters we override this |
106 | 111 |
|
107 | | -keysOfInterest = ones(1,256); |
| 112 | +keysOfInterest = zeros(1,256); |
108 | 113 |
|
109 | | -fprintf('\n Will be listening for key presses on : ') |
| 114 | +fprintf('\n Will be listening for key presses on : '); |
110 | 115 |
|
111 | | -if isfield(expParameters, 'responseKey') && ~isempty(expParameters.responseKey) |
| 116 | +if ~isempty(keyboard.responseKey) |
112 | 117 |
|
113 | | - responseTargetKeys = nan(1,numel(expParameters.responseKey)); |
| 118 | + responseTargetKeys = nan(1,numel(keyboard.responseKey)); |
114 | 119 |
|
115 | | - for iKey = 1:numel(expParameters.responseKey) |
116 | | - fprintf('\n - %s ', expParameters.responseKey{iKey}) |
117 | | - responseTargetKeys(iKey) = KbName(expParameters.responseKey(iKey)); |
| 120 | + for iKey = 1:numel(keyboard.responseKey) |
| 121 | + fprintf('\n - %s ', keyboard.responseKey{iKey}); |
| 122 | + responseTargetKeys(iKey) = KbName(keyboard.responseKey(iKey)); |
118 | 123 | end |
119 | 124 |
|
120 | 125 | keysOfInterest(responseTargetKeys) = 1; |
121 | 126 |
|
122 | 127 | else |
123 | 128 |
|
124 | | - fprintf('ALL KEYS.') |
| 129 | + keysOfInterest = ones(1,256); |
| 130 | + |
| 131 | + fprintf('ALL KEYS.'); |
125 | 132 |
|
126 | 133 | end |
127 | 134 |
|
128 | | -fprintf('\n\n') |
| 135 | +fprintf('\n\n'); |
129 | 136 | end |
130 | 137 |
|
131 | 138 |
|
132 | | -function responseEvents = getAllKeyEvents(responseEvents, responseBox, getOnlyPress) |
| 139 | +function responseEvents = getAllKeyEvents(responseEvents, deviceNumber, getOnlyPress) |
133 | 140 |
|
134 | 141 | iEvent = 1; |
135 | 142 |
|
136 | | -while KbEventAvail(responseBox) |
| 143 | +while KbEventAvail(deviceNumber) |
137 | 144 |
|
138 | | - event = KbEventGet(responseBox); |
| 145 | + event = KbEventGet(deviceNumber); |
139 | 146 |
|
140 | 147 | % we only return the pressed keys by default |
141 | 148 | if getOnlyPress==true && event.Pressed==0 |
|
0 commit comments