Getting keyboard input in MATLAB is fairly easy. For example, another new script. This script will wait till someone pushed either "a" or "l" and then returns which one was pressed.
clc;
clear all;
close all;
rng('shuffle');
% useful command, waits here until all keys on the keyboard are released
KbReleaseWait;
% lets define out target keys, this also gets the ASCII or numeric index for they key
k1 = 'a';
k2 = 'l';
keyOne = KbName(k1);
keyTwo = KbName(k2);
% start an infinite loop
while 1
% read the current status of the keyboard
[keyIsDown, secs, keyCode] = KbCheck;
% this returns some key information
% keyIsDown is a variable that is a 1 if a key is pressed and 0 if no key is pressed
% secs is the time of the key press
% keyCode is a vector for each key, a 1 will appear in the vector for the key that has been pressed
% this is a logic test, if the value of keyCode corresponding to 'a' is pressed
if keyCode(keyOne) == 1
disp([k1 ' was pressed']);
break;
end
if keyCode(keyTwo) == 1
disp([k2 ' was pressed']);
break;
end
end
This code is also HERE to help you out.
Now, let's make this code a bit more sophisticated. Let's build in a tone that occurs and then returns the reaction time it takes to press the space bar. Note, to do this we will be using tic and toc. tic starts a timer, toc returns the time in seconds (very precisely) when the toc command is given since the last tic. Before you start the code below try tic and toc on the MATLAB command line.
clc;
clear all;
close all;
rng('shuffle');
% useful command, waits here until all keys on the keyboard are released
KbReleaseWait;
% lets define out target keys, this also gets the ASCII or numeric index for they key
k1 = 'space';
keyOne = KbName(k1);
% start a clock using the tic and doc commands these are very useful
tic;
% play a beep
Beeper(600,0.6 ,0.05);
% start an infinite loop
while 1
% read the current status of the keyboard
[keyIsDown, secs, keyCode] = KbCheck;
% this returns some key information
% keyIsDown is a variable that is a 1 if a key is pressed and 0 if no key is pressed
% secs is the time of the key press
% keyCode is a vector for each key, a 1 will appear in the vector for the key that has been pressed
% this is a logic test, if the value of keyCode corresponding to 'a' is pressed
if keyCode(keyOne) == 1
% uses toc to get the time difference from tic
reaction_time = toc;
break;
end
end
disp(['Your reaction time was ' num2str(reaction_time) ' seconds']);
Again, to help you out my code is HERE.
Things to Try
1. Try using a different tone for the reaction time code above.
2. Try using a different input key for the reaction time task.
3. Advanced Challenge. Try to change the code above to do 10 trials of a reaction time task. You will need to use WaitSecs to build some time in between trials.
Note, MATLAB and PSYCHTOOLBOX supports a wide range of input devices. For example, in my lab we use a Gamepad. This can be read simply by using:
results = Gamepads;
Note, you have to have a Gamepad plugged in for this call to work. Also note, the very first time you call a function in can be quite slow. As such, you should call Gamepad for instance at the top of your program to initialize the function.
The MOUSE is easy to read. The following command reads the mouse/trackpad and returns the x position, y position, and the status of the buttons:
[x y buttons] = GetMouse;
Things to Try
4. Modify your reaction time code to wait for one of the mouse buttons to be pressed to get a reaction time value.
TRY THIS ON YOUR OWN! But if you need some help HERE is my code for this.
We are now ready to move onto a more complicated activity - in the next tutorial we will combine everything and build a trial in a choice reaction time task.
clc;
clear all;
close all;
rng('shuffle');
% useful command, waits here until all keys on the keyboard are released
KbReleaseWait;
% lets define out target keys, this also gets the ASCII or numeric index for they key
k1 = 'a';
k2 = 'l';
keyOne = KbName(k1);
keyTwo = KbName(k2);
% start an infinite loop
while 1
% read the current status of the keyboard
[keyIsDown, secs, keyCode] = KbCheck;
% this returns some key information
% keyIsDown is a variable that is a 1 if a key is pressed and 0 if no key is pressed
% secs is the time of the key press
% keyCode is a vector for each key, a 1 will appear in the vector for the key that has been pressed
% this is a logic test, if the value of keyCode corresponding to 'a' is pressed
if keyCode(keyOne) == 1
disp([k1 ' was pressed']);
break;
end
if keyCode(keyTwo) == 1
disp([k2 ' was pressed']);
break;
end
end
This code is also HERE to help you out.
Now, let's make this code a bit more sophisticated. Let's build in a tone that occurs and then returns the reaction time it takes to press the space bar. Note, to do this we will be using tic and toc. tic starts a timer, toc returns the time in seconds (very precisely) when the toc command is given since the last tic. Before you start the code below try tic and toc on the MATLAB command line.
clc;
clear all;
close all;
rng('shuffle');
% useful command, waits here until all keys on the keyboard are released
KbReleaseWait;
% lets define out target keys, this also gets the ASCII or numeric index for they key
k1 = 'space';
keyOne = KbName(k1);
% start a clock using the tic and doc commands these are very useful
tic;
% play a beep
Beeper(600,0.6 ,0.05);
% start an infinite loop
while 1
% read the current status of the keyboard
[keyIsDown, secs, keyCode] = KbCheck;
% this returns some key information
% keyIsDown is a variable that is a 1 if a key is pressed and 0 if no key is pressed
% secs is the time of the key press
% keyCode is a vector for each key, a 1 will appear in the vector for the key that has been pressed
% this is a logic test, if the value of keyCode corresponding to 'a' is pressed
if keyCode(keyOne) == 1
% uses toc to get the time difference from tic
reaction_time = toc;
break;
end
end
disp(['Your reaction time was ' num2str(reaction_time) ' seconds']);
Again, to help you out my code is HERE.
Things to Try
1. Try using a different tone for the reaction time code above.
2. Try using a different input key for the reaction time task.
3. Advanced Challenge. Try to change the code above to do 10 trials of a reaction time task. You will need to use WaitSecs to build some time in between trials.
Note, MATLAB and PSYCHTOOLBOX supports a wide range of input devices. For example, in my lab we use a Gamepad. This can be read simply by using:
results = Gamepads;
Note, you have to have a Gamepad plugged in for this call to work. Also note, the very first time you call a function in can be quite slow. As such, you should call Gamepad for instance at the top of your program to initialize the function.
The MOUSE is easy to read. The following command reads the mouse/trackpad and returns the x position, y position, and the status of the buttons:
[x y buttons] = GetMouse;
Things to Try
4. Modify your reaction time code to wait for one of the mouse buttons to be pressed to get a reaction time value.
TRY THIS ON YOUR OWN! But if you need some help HERE is my code for this.
We are now ready to move onto a more complicated activity - in the next tutorial we will combine everything and build a trial in a choice reaction time task.