how to count key presses in limited time?
11 views (last 30 days)
Show older comments
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..
3 Comments
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?
Accepted Answer
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
figure('KeyPressFcn',@youPressedSomething)
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)
Example:
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;
%Timer
t = timer('StartDelay',10,'TasksToExecute',1,'StartFcn',@timerStarted,...
'TimerFcn',@timerFinished);
%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!']);
else
startTimer;
end
end
end
%Kick off the timer!
function startTimer
start(t);
end
%Callback for when timer starts
function timerStarted(~,~)
count = 0;
allowCounting = true;
end
%Callback for when timer finishes
function timerFinished(~,~)
allowCounting = false;
end
%Cleanup (delete timer object)
function cleanUpTimer(~,~)
stop(t);
delete(t);
end
end
2 Comments
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
set(handles.figure,'DeleteFcn',@cleanUp,...)
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!
More Answers (1)
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
S.tm = 0;
S.tm(2) = 1; % These hold the timings.
S.fh = figure('KeyPressFcn',@youPressedSomething,...
'menu','none',...
'pos',[400 400 320 50]);
S.th = 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
S.tm(2) = now; % Holds current time.
if diff(S.tm)*86400>10 % Greater than 10 secs?
S.tm(1) = now;
S.cnt = 0;
end
if strcmp(varargin{2}.Character,' ')
S.cnt = S.cnt + 1;
set(S.th,'str',sprintf('You hit space %i times brah!',S.cnt));
end
end
end
2 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!