|
| 1 | +%% Demo showing how to use the getResponse function |
| 2 | + |
| 3 | +% This small script shows how to use the getReponse function |
| 4 | +% (a wrapper around the KbQueue function from PTB) |
| 5 | + |
| 6 | +% start with a clean slate |
| 7 | +clear; clc; |
| 8 | +if IsOctave |
| 9 | + more off % for a better display experience |
| 10 | +end |
| 11 | + |
| 12 | +%% set parameters |
| 13 | + |
| 14 | +% cfg.responseBox would be the device number of the device used by the participant to give his/her |
| 15 | +% response: like the button box in the scanner or a separate keyboard for a behavioral experiment |
| 16 | +% |
| 17 | +% cfg.keyboard would be the device number of the keyboard on which the experimenter will type or |
| 18 | +% press the keys necessary to start or abort the experiment. |
| 19 | + |
| 20 | +% cfg.responseBox and cfg.keyboard can be different or the same. |
| 21 | + |
| 22 | +% If you want to know the device number of all the keyboards and responses |
| 23 | +% boxes connected to the computer you can use the following code. |
| 24 | +% [cfg.keyboardNumbers, cfg.keyboardNames] = GetKeyboardIndices |
| 25 | + |
| 26 | +% Using empty vectors should work for linux when to select the "main" |
| 27 | +% keyboard. You might have to try some other values for Windows. To |
| 28 | +% assigne a specific keyboard input the kb assigned value (see README) |
| 29 | +cfg.keyboard = []; |
| 30 | +cfg.responseBox = []; |
| 31 | + |
| 32 | +% We set which keys are "valid", any keys other than those will be ignored |
| 33 | +expParameters.responseKey = {}; |
| 34 | + |
| 35 | + |
| 36 | +%% init |
| 37 | + |
| 38 | +% Keyboard |
| 39 | +% Make sure keyboard mapping is the same on all supported operating systems |
| 40 | +% Apple MacOS/X, MS-Windows and GNU/Linux: |
| 41 | +KbName('UnifyKeyNames'); |
| 42 | + |
| 43 | + |
| 44 | +% we ask PTB to tell us which keyboard devices are connected to the computer |
| 45 | +[cfg.keyboardNumbers, cfg.keyboardNames] = GetKeyboardIndices; |
| 46 | + |
| 47 | +cfg.keyboardNumbers |
| 48 | +cfg.keyboardNames |
| 49 | + |
| 50 | + |
| 51 | +% Test that the keyboards are correctly configured |
| 52 | +testKeyboards(cfg); |
| 53 | + |
| 54 | +% Give the time to the test key to be released and not listened |
| 55 | +WaitSecs(1); |
| 56 | + |
| 57 | + |
| 58 | +fprintf('\nDuring the next 5 seconds we will collect responses on the following keys: \n\n'); |
| 59 | +if isempty(expParameters.responseKey) |
| 60 | + fprintf('\nALL KEYS\n\n'); |
| 61 | +else |
| 62 | + for iKey=1:numel(expParameters.responseKey) |
| 63 | + fprintf('\n%s', expParameters.responseKey{iKey}); |
| 64 | + end |
| 65 | + fprintf('\n\n'); |
| 66 | +end |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +%% Run demo |
| 71 | + |
| 72 | +% Create the keyboard queue to collect responses. |
| 73 | +getResponse('init', cfg, expParameters, 1); |
| 74 | + |
| 75 | +% Start collecting responses for 5 seconds |
| 76 | +% Each new key press is added to the queue of events recorded by KbQueue |
| 77 | +startSecs = GetSecs(); |
| 78 | +getResponse('start', cfg, expParameters, 1); |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +% Here we wait for 5 seconds but are still collecting responses. |
| 83 | +% So you could still be doing something else (presenting audio and visual stim) and |
| 84 | +% still collect responses. |
| 85 | +WaitSecs(5); |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +% Check what keys were pressed (all of them) |
| 90 | +responseEvents = getResponse('check', cfg, expParameters, 0); |
| 91 | + |
| 92 | +% The following line would only return key presses and not releases |
| 93 | +% responseEvents = getResponse('check', cfg, expParameters, 1); |
| 94 | + |
| 95 | +% This can be used to flush the queue: empty all events that are still present in the queue |
| 96 | +getResponse('flush', cfg, expParameters, 1); |
| 97 | + |
| 98 | +% If you wan to stop listening to key presses. |
| 99 | +getResponse('stop', cfg, expParameters, 1); |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +%% Now we look what keys were pressed and when |
| 105 | +for iEvent = 1:size(responseEvents, 1) |
| 106 | + |
| 107 | + if responseEvents(iEvent).pressed |
| 108 | + eventType = 'pressed'; |
| 109 | + else |
| 110 | + eventType = 'released'; |
| 111 | + end |
| 112 | + |
| 113 | + fprintf('%s was %s at time %.3f seconds\n', ... |
| 114 | + responseEvents(iEvent).key_name, ... |
| 115 | + eventType, ... |
| 116 | + responseEvents(iEvent).onset - startSecs); |
| 117 | + |
| 118 | +end |
0 commit comments