how to count key presses in limited time?
이전 댓글 표시
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
Azzi Abdelmalek
2012년 9월 9일
what do you mean? to count during 10 seconds the times the subject pressed on the spacebar
Jan
2012년 9월 9일
"now- where in the code (in which function)": Is there any chance that we can answer this, without knowing your code?
alex
2012년 10월 10일
채택된 답변
추가 답변 (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
Jarrod Rivituso
2012년 10월 10일
and you kept the brah thing going! :)
Matt Fig
2012년 10월 10일
Gotta love the brah, brah!
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!