フィルターのクリア

how to count key presses in limited time?

4 ビュー (過去 30 日間)
alex
alex 2012 年 10 月 12 日
its aquestion I've asked before, and got some answers, but I'v just found out that it doesnt count key presses- it counts also if I just hold the key pressed wthout releasing it- and thats no good...
this is what I have now- and I want to fix somehow-
%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
set(a8,'Visible','off');
set(a9,'Visible','on');
set(th,'Visible','on','String',[ num2str(count) ]);
if strcmp(eventdata.Character,' ') && flag
if allowCounting
count = count+1;
set(th,'Visible','on','String',[ num2str(count) ]);
else
startTimer;
end
end
end
%Kick off the timer!
function startTimer
start(t);
end
%Callback for when timer starts
function timerStarted(~,~)
count = 1;
allowCounting = true;
end
%Callback for when timer finishes
function timerFinished(~,~)
......

採用された回答

Matt Fig
Matt Fig 2012 年 10 月 12 日
編集済み: Matt Fig 2012 年 10 月 13 日
I would do it just like I showed you before, but add a keyreleasefcn. So when the user pushes the correct key, a value is set. Subsequent calls to the keypressfcn are ignored unless the value is unset by the keyreleasefcn. For example (recall you may want to use windowkeypressfcn and windowkeyreleasefcn instead...):
function youPressedSpaceBrah2
%Create figure and text box
S.tm = 0;
S.tm(2) = 1;
S.fh = figure('KeyPressFcn',@youPressedSomething,...
'KeyReleaseFcn',@youReleasedSomething,...
'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;
S.R = 1;
function youPressedSomething(varargin)
%See if it is the space bar
if ~S.R
return
else
S.R = 0;
end
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
function youReleasedSomething(varargin)
S.R = 1;
end
end
.
.
.
Here is another example. This one does not use nested functions, so it is more like what you might find when working with at GUI built by GUIDE. Notice that I use the GUIDATA function throughout. Your code will differ, of course, but I think you can get the idea....
.
.
.
function youPressedSpaceBrah3
%Create figure and text box
S.tm = 0;
S.tm(2) = 1;
S.fh = figure('KeyPressFcn',@youPressedSomething,...
'KeyReleaseFcn',@youReleasedSomething,...
'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;
S.R = 1;
guidata(S.fh,S) % Store the handles.
function youPressedSomething(varargin)
%See if it is the space bar
S = guidata(gcbf); % Get the structure
if ~S.R
return % User has not released the spacebar....
else
S.R = 0;
end
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
guidata(gcbf,S) % Save the structure
function youReleasedSomething(varargin)
S = guidata(gcbf); % Get the structure
S.R = 1;
guidata(gcbf,S) % Save the structure
  1 件のコメント
alex
alex 2012 年 10 月 13 日
thanks.. it was just what I need to finish this first GUI..

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDialog Boxes についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by