how to count key presses in limited time?

10 次查看(过去 30 天)
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 个评论
Jan
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
alex 2012-10-10
o.k I will make myself more clear- I have solved some of my problems, but there are a gew unsolved. I will add my code below, it doesnt work for now but it it easy to see from the code what I want. 1. how can I count spacebar presses? I have this code (ignore the parts of writing to exel files)-
k=0;
a=0;
counter=1;
while k==0
k=waitforbuttonpress
if k==1
tic
while a<10
a=toc;
if event.key=='spacebar'
counter=counter+1;
return
end
end
end
end

请先登录,再进行评论。

采纳的回答

Jarrod Rivituso
Jarrod Rivituso 2012-10-10
编辑:Jarrod Rivituso 2012-10-10
Oh, you need timer objects and a key press function brah! Also, you gotta stop thinking in loops and start thinking in callbacks!
Check out the example below...
Some key points
1. Listen to the figure to see when a key is pressed
figure('KeyPressFcn',@youPressedSomething)
2. Use a timer object with callbacks that control a state variable
t = timer(...)
There's good documentation on both these things brah!
Also I made a typo when typing "spacebar" and changed it to "spacebrah" and thought it was funny so I kept it going in this post (hence me saying "brah" a lot)
Example:
function youPressedSpaceBrah
%Create figure and text box
fh = figure('DeleteFcn',@cleanUpTimer,'KeyPressFcn',@youPressedSomething);
th = uicontrol('Parent',fh,'Style','text','Position',[10 10 300 30],...
'String','You have not hit space yet');
%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
if strcmp(eventdata.Character,' ')
if allowCounting
count = count+1;
set(th,'String',['You hit space ' num2str(count) ' times brah!']);
else
startTimer;
end
end
end
%Kick off the timer!
function startTimer
start(t);
end
%Callback for when timer starts
function timerStarted(~,~)
count = 0;
allowCounting = true;
end
%Callback for when timer finishes
function timerFinished(~,~)
allowCounting = false;
end
%Cleanup (delete timer object)
function cleanUpTimer(~,~)
stop(t);
delete(t);
end
end
  2 个评论
alex
alex 2012-10-10
first of all you are totaly right with the loops and the callbacks issue.. I'm just new in gui stuff and loops are much more easier to me..
I do have two questions-
1. your code doesn't seem to work- no figure is shown when I run it..
2. since I use a code created by (matlab itself) saving a work on GUI window,and I just add my callbacks. I am not sure where to put those commands such as-
...'DeleteFcn',@cleanUpTimer,'KeyPressFcn',@youPressedSomething)
so as you can see it looks different than the one you wrote, so where should I put those changes ?
Jarrod Rivituso
Jarrod Rivituso 2012-10-11
hi Alex (aka brah!)
you are using guide. guide has a model where all of the callbacks are typically subfunctions, and handle graphics objects are referenced via the handles input.
so, you would write something like
set(handles.figure,'DeleteFcn',@cleanUp,...)
and then of course you would write the cleanUp function :)
Also, the OpeningFcn callback (called alex2_OpeningFcn in your code) is a good place to define the timer object and state variables. Then, you just need to pass those variables to your callbacks via the handles structure.
Luckily, Mr. Doug Hull has a blog post on how to do this with GUIDE brah!
Also, if you ever decide to go without GUIDE and use the nested function approach I've shown, ~the~ Matt Fig (brah #2) has provided a bunch of awesome GUI examples for us that we can use as reference:
Hope this helps brah!

请先登录,再进行评论。

更多回答(1 个)

Matt Fig
Matt Fig 2012-10-10
编辑:Matt Fig 2012-10-10
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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by