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!