how to count key presses in limited time?
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
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
Accepted Answer
More Answers (1)
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
Jarrod Rivituso
on 10 Oct 2012
and you kept the brah thing going! :)
Matt Fig
on 10 Oct 2012
Gotta love the brah, brah!
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!