Skip to content

Commit f012d2a

Browse files
committed
refactor getResponse
1 parent 0c82bfa commit f012d2a

File tree

1 file changed

+55
-32
lines changed

1 file changed

+55
-32
lines changed

getResponse.m

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function responseEvents = getResponse(action, cfg, expParameters, getOnlyPress, verbose)
1+
function responseEvents = getResponse(action, cfg, expParameters, 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.
@@ -16,29 +16,29 @@
1616
% - flush:
1717
% - stop:
1818
%
19-
% - getOnlyPress: if set to 1 the function will only return the key presses and
20-
% will not return when the keys were released (default=1)
19+
% - getOnlyPress: if set to 1 the function will only return the key presses and
20+
% will not return when the keys were released (default=1)
2121
% See the section on OUTPUT below for more info
2222
%
2323
%
24-
%
24+
%
2525
% OUTPUT
26-
%
26+
%
2727
% responseEvents: returns all the keypresses and return them as a structure
2828
% with field names that make it easier to save the output of in a BIDS
2929
% format
30-
%
30+
%
3131
% responseEvents.onset : this is an absolute value and you should
3232
% substract the "experiment start time" to get a value relative to when the
3333
% experiment was started.
34-
%
34+
%
3535
% responseEvents.trial_type = 'response';
36-
%
36+
%
3737
% responseEvents.duration = 0;
38-
%
38+
%
3939
% responseEvents.key_name : the name of the key pressed
40-
%
41-
% responseEvents(iEvent,1).pressed : if
40+
%
41+
% responseEvents(iEvent,1).pressed : if
4242
% pressed == 1 --> the key was pressed
4343
% pressed == 0 --> the key was released
4444

@@ -47,10 +47,6 @@
4747
getOnlyPress = 1;
4848
end
4949

50-
if nargin < 5
51-
verbose = 0;
52-
end
53-
5450
responseEvents = struct;
5551
responseEvents.onset = [];
5652
responseEvents.trial_type = [];
@@ -76,7 +72,7 @@
7672

7773
% by default we listen to all keys
7874
% but if responseKey is set in the parameters we override this
79-
keysOfInterest = ones(1,256);
75+
keysOfInterest = ones(1,256);
8076

8177
fprintf('\n Will be listening for key presses on : ')
8278

@@ -90,7 +86,7 @@
9086
end
9187

9288
keysOfInterest(responseTargetKeys) = 1;
93-
89+
9490
else
9591

9692
fprintf('ALL KEYS.')
@@ -105,19 +101,13 @@
105101

106102
case 'start'
107103

108-
fprintf('\n starting to listen to keypresses\n')
109-
110104
KbQueueStart(responseBox);
111105

112106

113107
case 'check'
114108

115-
if verbose
116-
fprintf('\n checking recent keypresses\n')
117-
end
118-
119109
iEvent = 1;
120-
110+
121111
while KbEventAvail(responseBox)
122112

123113
event = KbEventGet(responseBox);
@@ -131,32 +121,65 @@
131121
responseEvents(iEvent,1).duration = 0;
132122
responseEvents(iEvent,1).key_name = KbName(event.Keycode);
133123
responseEvents(iEvent,1).pressed = event.Pressed;
134-
124+
135125
end
136126

137127
iEvent = iEvent + 1;
138128

139129
end
140130

131+
141132
case 'flush'
142133

143-
if verbose
144-
fprintf('\n reinitialising keyboard queue\n')
145-
end
146-
147134
KbQueueFlush(responseBox);
148135

149136

150137
case 'stop'
151138

152-
fprintf('\n stopping to listen to keypresses\n\n')
153-
154139
KbQueueRelease(responseBox);
155140

156141
% Give me my keyboard back... Pretty please.
157142
ListenChar(0);
158143

144+
145+
end
146+
147+
talkToMe(action, expParameters);
148+
149+
159150
end
160151

161152

162-
end
153+
function talkToMe(action, expParameters)
154+
155+
if ~isfield(expParameters, 'verbose') || isempty(expParameters.verbose)
156+
expParameters.verbose = false;
157+
end
158+
159+
switch action
160+
161+
case 'init'
162+
163+
case 'start'
164+
165+
fprintf('\n starting to listen to keypresses\n')
166+
167+
case 'check'
168+
169+
if expParameters.verbose
170+
fprintf('\n checking recent keypresses\n')
171+
end
172+
173+
case 'flush'
174+
175+
if expParameters.verbose
176+
fprintf('\n reinitialising keyboard queue\n')
177+
end
178+
179+
case 'stop'
180+
181+
fprintf('\n stopping to listen to keypresses\n\n')
182+
183+
end
184+
185+
end

0 commit comments

Comments
 (0)