MATLAB Answers

how to count key presses in limited time?

36 views (last 30 days)
my task is using a GUI, to count during 10 seconds the times the subject pressed on the spacebar . after building a GUI that gets the subject's id, it makes a text visible that says- " the 10 seconds will start when you press the spacebar for the first time"
now- where in the code (in which function) I have to put the timer and counter orders? and how do I make a counter that works for a limited time of x seconds.
I guess i need to use somehow the keypress and keyrelease functions although I am not too fimiliar with them..


Azzi Abdelmalek
Azzi Abdelmalek on 9 Sep 2012
what do you mean? to count during 10 seconds the times the subject pressed on the spacebar
Jan on 9 Sep 2012
"now- where in the code (in which function)": Is there any chance that we can answer this, without knowing your code?
alex on 10 Oct 2012
o.k I will make myself more clear- I have solved some of my problems, but there are a gew unsolved. I will add my code below, it doesnt work for now but it it easy to see from the code what I want. 1. how can I count spacebar presses? I have this code (ignore the parts of writing to exel files)-
while k==0
if k==1
while a<10
if event.key=='spacebar'

Sign in to comment.

Accepted Answer

Jarrod Rivituso
Jarrod Rivituso on 10 Oct 2012
Edited: Jarrod Rivituso on 10 Oct 2012
Oh, you need timer objects and a key press function brah! Also, you gotta stop thinking in loops and start thinking in callbacks!
Check out the example below...
Some key points
1. Listen to the figure to see when a key is pressed
2. Use a timer object with callbacks that control a state variable
t = timer(...)
There's good documentation on both these things brah!
Also I made a typo when typing "spacebar" and changed it to "spacebrah" and thought it was funny so I kept it going in this post (hence me saying "brah" a lot)
function youPressedSpaceBrah
%Create figure and text box
fh = figure('DeleteFcn',@cleanUpTimer,'KeyPressFcn',@youPressedSomething);
th = uicontrol('Parent',fh,'Style','text','Position',[10 10 300 30],...
'String','You have not hit space yet');
%State variables
count = 0;
allowCounting = false;
t = timer('StartDelay',10,'TasksToExecute',1,'StartFcn',@timerStarted,...
%Callback functions
%When the user presses a key
function youPressedSomething(~,eventdata)
%See if it is the space bar
if strcmp(eventdata.Character,' ')
if allowCounting
count = count+1;
set(th,'String',['You hit space ' num2str(count) ' times brah!']);
%Kick off the timer!
function startTimer
%Callback for when timer starts
function timerStarted(~,~)
count = 0;
allowCounting = true;
%Callback for when timer finishes
function timerFinished(~,~)
allowCounting = false;
%Cleanup (delete timer object)
function cleanUpTimer(~,~)


alex on 10 Oct 2012
first of all you are totaly right with the loops and the callbacks issue.. I'm just new in gui stuff and loops are much more easier to me..
I do have two questions-
1. your code doesn't seem to work- no figure is shown when I run it..
2. since I use a code created by (matlab itself) saving a work on GUI window,and I just add my callbacks. I am not sure where to put those commands such as-
so as you can see it looks different than the one you wrote, so where should I put those changes ?
Jarrod Rivituso
Jarrod Rivituso on 11 Oct 2012
hi Alex (aka brah!)
you are using guide. guide has a model where all of the callbacks are typically subfunctions, and handle graphics objects are referenced via the handles input.
so, you would write something like
and then of course you would write the cleanUp function :)
Also, the OpeningFcn callback (called alex2_OpeningFcn in your code) is a good place to define the timer object and state variables. Then, you just need to pass those variables to your callbacks via the handles structure.
Luckily, Mr. Doug Hull has a blog post on how to do this with GUIDE brah!
Also, if you ever decide to go without GUIDE and use the nested function approach I've shown, ~the~ Matt Fig (brah #2) has provided a bunch of awesome GUI examples for us that we can use as reference:
Hope this helps brah!

Sign in to comment.

More Answers (1)

Matt Fig
Matt Fig on 10 Oct 2012
Edited: Matt Fig on 10 Oct 2012
One could also do it without the use of a timer, simplifying things greatly. Note that I reused a lot of Jarrod's code...
function youPressedSpaceBrah2
%Create figure and text box = 0; = 1; % These hold the timings.
S.fh = figure('KeyPressFcn',@youPressedSomething,...
'pos',[400 400 320 50]); = uicontrol('Style','text','Position',[10 10 300 30],...
'String','You have not hit space yet');
S.cnt = 0;
%When the user presses a key
function youPressedSomething(varargin)
%See if it is the space bar = now; % Holds current time.
if diff(*86400>10 % Greater than 10 secs? = now;
S.cnt = 0;
if strcmp(varargin{2}.Character,' ')
S.cnt = S.cnt + 1;
set(,'str',sprintf('You hit space %i times brah!',S.cnt));


Jarrod Rivituso
Jarrod Rivituso on 10 Oct 2012
and you kept the brah thing going! :)
Matt Fig
Matt Fig on 10 Oct 2012
Gotta love the brah, brah!

Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!