-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtonSetup.m
226 lines (180 loc) · 6.49 KB
/
ButtonSetup.m
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
function keyfile = ButtonSetup()
KbName('UnifyKeyNames');
%reset devices
clear PsychHID;
clear KbCheck;
DisableKeysForKbCheck([]);
% all keys should be the integer values!
% defaults
keys.trigger = 52;
% These are referenced in the original initkeys, but this module doesn't
% change them. Not sure if they are still needed.
keys.space=KbName('SPACE');
keys.esc=KbName('ESCAPE');
keys.right=KbName('RightArrow');
keys.left=KbName('LeftArrow');
keys.up=KbName('UpArrow');
keys.down=KbName('DownArrow');
keys.shift=KbName('RightShift');
keys.kill = KbName('k');
% no! bad! redundant with b0-b9 (at best!)
% keys.buttons = (30:39);
f = uifigure('Name', 'Device Setup', ...
'KeyPressFcn', @myKeyPress, ...
'UserData', '');
label = uilabel(f);
label.FontSize = 16;
label.Position = [20 200 500 100];
label.Text = "Open keyboard mat file";
[indices, names, infos] = GetKeyboardIndices();
for kn = 1:length(indices)
KbQueueCreate(indices(kn));
KbQueueStart(indices(kn));
end
%% this part just caused problems
% response = uiconfirm(f, 'Open existing keyboard mat file?', 'Open file', ...
% 'Options',{'Yes','No'});
% if strcmp(response, 'Yes')
% keyfile = uigetfile('*.mat', 'Open file');
% if keyfile
% load(keyfile, keys);
% end
% end
%%
orig_keys = keys;
figure(f);
if isunix && ~ismac
label.Text = "Click on window title, then press any button on your keyboard";
else
label.Text = "Click anywhere in this space, then press any button on your keyboard";
end
[~, i] = WaitForKbPress(f, indices);
info = struct2table(infos{i}, 'AsArray', true);
if isunix && ~ismac
keys.keyboard_loc = info.interfaceID;
else
keys.keyboard_loc = info.locationID;
end
keys.keyboard_name = names(i);
label.Text = ["Press any button on the triggering device" ...
"(button control box for scanner, keyboard for mock or test mode)"];
[~, i] = WaitForKbPress(f, indices);
trigger_index = indices(i);
info = struct2table(infos{i}, 'AsArray', true);
if isunix && ~ismac
keys.trigger_loc = info.interfaceID;
else
keys.trigger_loc = info.locationID;
end
keys.trigger_name = names(i);
str1 = "Triggering device = " + keys.trigger_name;
str2 = "Trigger key is " + KbName(keys.trigger) ...
+ sprintf(' (%d)', keys.trigger);
str3 = "Press enter to confirm trigger key, space to change trigger key";
label.Text = [str1, str2, str3];
response = WaitForKeypress(f);
switch response
case 'escape'
quit
case 'space'
label.Text = "Prepare trigger device and press enter when ready";
response = '';
while not(strcmp(response, 'return'))
response = WaitForKeypress(f);
end
KbQueueFlush(trigger_index);
label.Text = "Waiting for trigger";
WaitForKeypress(f);
[~, keyCode] = KbQueueCheck(trigger_index);
label.Text = ["Trigger detected: " + KbName(keyCode) ...
+ sprintf(' (%d)', KbName(KbName(keyCode))), ...
"Press enter to continue."];
keys.trigger = KbName(KbName(keyCode));
response = '';
while not(strcmp(response, 'return'))
response = WaitForKeypress(f);
end
end
for kn = 1:length(indices)
KbQueueFlush(indices(kn));
end
str1 = "Press left response buttons from left to right: ";
label.Text = str1;
[keys.b0, i] = WaitForKbPress(f, indices);
info = struct2table(infos{i}, 'AsArray', true);
if isunix && ~ismac
keys.left_loc = info.interfaceID;
else
keys.left_loc = info.locationID;
end
keys.left_name = names(i);
label.Text = str1 + KbName(keys.b0);
keys.b1 = WaitForKbPress(f, indices);
label.Text = str1 + KbName(keys.b1);
keys.b2 = WaitForKbPress(f, indices);
label.Text = str1 + KbName(keys.b2);
keys.b3 = WaitForKbPress(f, indices);
label.Text = str1 + KbName(keys.b3);
keys.b4 = WaitForKbPress(f, indices);
%label.Text = str1 + KbName(keys.b4);
str2 = "Press right response buttons from left to right: ";
label.Text = [ str1 + KbName(keys.b4), str2];
[keys.b5, i] = WaitForKbPress(f, indices);
info = struct2table(infos{i}, 'AsArray', true);
% this might work better everywhere? but not tested yet
if isunix && ~ismac
keys.right_loc = info.interfaceID;
else
keys.right_loc = info.locationID;
end
keys.right_name = names(i);
label.Text = [ str1 + KbName(keys.b4), str2 + KbName(keys.b5)];
keys.b6 = WaitForKbPress(f, indices);
label.Text = [ str1 + KbName(keys.b4), str2 + KbName(keys.b6)];
keys.b7 = WaitForKbPress(f, indices);
label.Text = [ str1 + KbName(keys.b4), str2 + KbName(keys.b7)];
keys.b8 = WaitForKbPress(f, indices);
label.Text = [ str1 + KbName(keys.b4), str2 + KbName(keys.b8)];
keys.b9 = WaitForKbPress(f, indices);
label.Text = [ str1 + KbName(keys.b4), str2 + KbName(keys.b9)];
for kn = 1:length(indices)
KbQueueRelease(indices(kn));
end
if isequal(keys, orig_keys)
message = 'Keys unchanged. Write new file anyway?';
else
message = 'Keys changed. Write new file?';
end
response = uiconfirm(f, message, 'Confirm choice',...
'Options',{'Yes','No'});
delete(f);
if strcmp(response, 'Yes')
[filename, path] = uiputfile('*.mat', 'Save file');
keyfile = fullfile(path, filename);
if filename
save(keyfile, 'keys');
end
end
end
function myKeyPress(hFig, EventData)
set(hFig, 'UserData', EventData.Key)
end
% wait for a keypress and return it, matlab style
function keyname = WaitForKeypress(hFig)
set(hFig, 'UserData', '');
waitfor(hFig, 'UserData');
keyname = hFig.UserData;
end
% wait for a keypress and get the device & key using psychtoolbox
% returned index must be converted to device number/name!
% keyname will be the int version
function [keyname, device] = WaitForKbPress(hFig, device_indices)
WaitForKeypress(hFig);
for kn = 1:length(device_indices)
[pressed, firstpress] = KbQueueCheck(device_indices(kn));
if pressed
keyname = KbName(KbName(firstpress));
device = kn;
end
end
end