Skip to content

Commit 90dcc06

Browse files
committed
update getResponse
- can listen to a specific device - all information about is passed through the cfg structure even info about restricted keys - information are passed through cfg.keyboard
1 parent 33ea857 commit 90dcc06

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

getResponse.m

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function responseEvents = getResponse(action, cfg, expParameters, getOnlyPress)
1+
function responseEvents = getResponse(action, deviceNumber, cfg, getOnlyPress)
22
% wrapper function to use KbQueue
33
% The queue will be listening to key presses on the response box as defined
44
% in the cfg structure : see setParameters for more details.
@@ -43,14 +43,22 @@
4343
% pressed == 0 --> the key was released
4444

4545

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+
4656
if nargin < 4
4757
getOnlyPress = true;
4858
end
4959

5060
responseEvents = struct;
5161

52-
responseBox = cfg.responseBox;
53-
5462
switch action
5563

5664
case 'init'
@@ -60,82 +68,81 @@
6068
ListenChar(-1);
6169

6270
% Clean and realease any queue that might be opened
63-
KbQueueRelease(responseBox);
71+
KbQueueRelease(deviceNumber);
6472

65-
keysOfInterest = setKeysOfInterest(expParameters);
73+
keysOfInterest = setKeysOfInterest(cfg.keyboard);
6674

6775
% Create the keyboard queue to collect responses.
68-
KbQueueCreate(responseBox, keysOfInterest);
69-
70-
71-
case 'start'
72-
73-
KbQueueStart(responseBox);
76+
KbQueueCreate(deviceNumber, keysOfInterest);
7477

78+
case 'start'
79+
KbQueueStart(deviceNumber);
7580

7681
case 'check'
82+
responseEvents = getAllKeyEvents(responseEvents, deviceNumber, getOnlyPress);
7783

7884
responseEvents = getAllKeyEvents(responseEvents, responseBox, getOnlyPress);
7985

8086
checkAbort(cfg)
8187

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)
8893

89-
KbQueueRelease(responseBox);
94+
case 'release'
95+
KbQueueRelease(deviceNumber);
9096

9197
% Give me my keyboard back... Pretty please.
9298
ListenChar(0);
93-
94-
99+
95100
end
96101

97-
talkToMe(action, expParameters);
102+
talkToMe(action);
98103

99104
end
100105

101106

102-
function keysOfInterest = setKeysOfInterest(expParameters)
107+
function keysOfInterest = setKeysOfInterest(keyboard)
103108
% list all the response keys we want KbQueue to listen to
104109
% by default we listen to all keys
105110
% but if responseKey is set in the parameters we override this
106111

107-
keysOfInterest = ones(1,256);
112+
keysOfInterest = zeros(1,256);
108113

109-
fprintf('\n Will be listening for key presses on : ')
114+
fprintf('\n Will be listening for key presses on : ');
110115

111-
if isfield(expParameters, 'responseKey') && ~isempty(expParameters.responseKey)
116+
if ~isempty(keyboard.responseKey)
112117

113-
responseTargetKeys = nan(1,numel(expParameters.responseKey));
118+
responseTargetKeys = nan(1,numel(keyboard.responseKey));
114119

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));
118123
end
119124

120125
keysOfInterest(responseTargetKeys) = 1;
121126

122127
else
123128

124-
fprintf('ALL KEYS.')
129+
keysOfInterest = ones(1,256);
130+
131+
fprintf('ALL KEYS.');
125132

126133
end
127134

128-
fprintf('\n\n')
135+
fprintf('\n\n');
129136
end
130137

131138

132-
function responseEvents = getAllKeyEvents(responseEvents, responseBox, getOnlyPress)
139+
function responseEvents = getAllKeyEvents(responseEvents, deviceNumber, getOnlyPress)
133140

134141
iEvent = 1;
135142

136-
while KbEventAvail(responseBox)
143+
while KbEventAvail(deviceNumber)
137144

138-
event = KbEventGet(responseBox);
145+
event = KbEventGet(deviceNumber);
139146

140147
% we only return the pressed keys by default
141148
if getOnlyPress==true && event.Pressed==0

0 commit comments

Comments
 (0)