how to capture each time the key is pressed.in my code when second key is pressed it adds the time of first keypresssed also
5 views (last 30 days)
Show older comments
function key_pressFcn
figure('KeyPressFcn',@cb)
tic;
function cb(src,evnt)
out = sprintf('Key: %s\n',evnt.Key);
disp(out)
time=toc;
disp(time)
0 Comments
Accepted Answer
Geoff Hayes
on 5 Jun 2014
toc works as follows (type help roc or doc toc in the Command Window for details)
TIC and toc functions work together to measure elapsed time.
toc, by itself, displays the elapsed time, in seconds, since
the most recent execution of the TIC command.
T = toc; saves the elapsed time in T as a double scalar.
The above code only calls tic the one time - when you run the key_pressFcn that creates the figure, adds the key press callback to the figure, and sets tic. So every time you press a key when the figure has focus, the callback cb gets called, it outputs the key pressed, and calls toc which returns the elapsed time since tic was called creating the impression that the time of the first key press is added to the second key press. This behaviour is expected given the nature of tic and toc.
If you want the elapsed time between the last key press, then you could modify your callback to include tic within it (so that it doesn't conflict with any other calls outside of this callback to tic)
function cb(src,evnt)
persistent ticStartTime; % creates a persistent variable
out = sprintf('Key: %s\n',evnt.Key);
disp(out)
if isempty(ticStartTime)
% use last the tic from the key_pressFcn
time=toc;
else
% use tic that was called within this callback
time = toc(ticStartTime);
end
disp(time)
% restart tic for this function
ticStartTime = tic;
So the above will give the elapsed time between key presses within the figure. NOTE that as ticStartTime is persistent, it will remain so long as the key_pressFcn.m file is not clear or not changed. So subsequent executions of key_pressFcn will use the last known value of ticStartTime for the initial key press within the figure. Type help persistent for details.
Note that is you just want the current time that the key is pressed, then just set time to now:
time = now;
12 Comments
Geoff Hayes
on 12 Jun 2014
puja - what have you tried so far? Please show the attempt that you have made to solve your problem by posting some working code that you have written so far. From that, I will try to offer suggestions on what to do next.
More Answers (1)
puja dutta
on 13 Jun 2014
1 Comment
Geoff Hayes
on 13 Jun 2014
Puja - the code that you have posted above does not work at all. It is not much different from the version you posted in a previous comment. See my response to that in the comment dated 9 Jun 2014 at 0:20.
Before posting, please make sure that the code can be run. It may not do what you want (that is fine for now) but at least make sure that is doesn't fail as soon as the function is called from the command line.
And if you are getting an error message, please post that message.
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!