how to count key presses in limited time?

2 views (last 30 days)
alex
alex on 12 Oct 2012
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(~,~)
......

Accepted Answer

Matt Fig
Matt Fig on 12 Oct 2012
Edited: Matt Fig on 13 Oct 2012
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 Comment
alex
alex on 13 Oct 2012
thanks.. it was just what I need to finish this first GUI..

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!