필터 지우기
필터 지우기

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 CenterFile Exchange에서 Dialog Boxes에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by