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

function key_pressFcn
figure('KeyPressFcn',@cb)
tic;
function cb(src,evnt)
out = sprintf('Key: %s\n',evnt.Key);
disp(out)
time=toc;
disp(time)

 Accepted Answer

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

thanks alot..it was really helpful. can you please help me to tell how can i calculate the time interval when one key is released and next key is pressed. i am unable to differentiate between two key. can keypressfcn can be called two times for two different key
If you want to calculate the difference in time between when one key is released and another is pressed, then you could use the KeyReleaseFcn callback paired with the KeyPressFcn callback
h = figure;
set(h,'KeyPressFcn',@keyPressCb,'KeyReleaseFcn',@keyReleaseCb);
In the keyReleaseCb callback, the code could call tic and the the keyPressCb could call toc. You would not be able to use the persistent variable above since now you are sharing data between the two callbacks. A global variable could be used in place of this persistent one to capture the tic start time (to avoid conflicts with other calls to tic.) Note also that additional logic would be needed to capture the event where the user has not released the first key and keeps pressing additional keys - so multiple key press events occur for no key release ones.
thanks a alot....its gonna help me alot..i was really blank on this...i am gonna try it now and i hope you will help again if i get stuck in this again.
I have tried alot but not getting any idea what logic to add to capture the event when the user has not released the first key.i made a code like
function keypress_Test
h = figure;
set(h,'KeyPressFcn',@keyPressCb,'KeyReleaseFcn',@keyReleaseCb);
tic;
function keyReleaseCb(src,evnt)
global ticStartTime;
function KeyPressCb(src,evnt)
time=toc(ticStartTime);
end
disp(time)
ticStartTime=tic;
before Keyrelease callback have to add something for first Keypress and capture the event till it is released.but i dont know how to do that.please help me.I would be great full if you help me.
Is the above code meant to work, or is it just an example? If you keep all three functions in one file, then you must conclude each function with the end keyword:
function keypress_Test
h = figure;
set(h,'KeyPressFcn',@keyPressCb,'KeyReleaseFcn',@keyReleaseCb);
end
function keyReleaseCb(src,evnt)
% do stuff here to handle key release
end
function keyPressCb(src,evnt)
% do stuff here to handle key press
end
Note the spelling of the last function - it begins with a lower case 'k' to match that of the defined callback in the keypress_Test function.
Does it matter if the user has not released the last pressed key? I.e. is this important to solving your problem? If not, then I would just ignore it. But if you do need to keep track of whether the last key pressed has been released or not, then you will need to keep track of the system "state" using a global variable, for example the number of keys that have been pressed. Take a look at the documentation on how to define and use global variables http://www.mathworks.com/help/matlab/ref/global.html, and try again with this. But only if you really need to handle this event...
yes it is important.Actually i want to capture the time interval between the key is released and next key is pressed.But i am stuck in how to capture the event until the first key is released. I tried to use keypressfcn for the first key but then keypresscb for the next key doesnt work.
you told me in to add some logic to capture the event where the user has not released first key and keeps pressing additional keys - so multiple key press events occur for no key release ones.I am not getting any idea to add what logic.can u please help me by telling the logic please..
What have you tried so far? (Please show your code.) You could use some sort of flag that is set to true when a key is pressed and set to false when a key is released. If the flag is still true when a new key is pressed then that would mean that the previous key has yet to be released (but that may cause problems when the second key is released but not the first). You may need to keep track of all keys pressed (using evnt.Key) and then note when they have been released.
Are you absolutely sure that this sort of logic is necessary to solve your problem? It seems that it could get complicated quickly and that you might be spending too much time on something that is not strictly necessary (trying to introduce logic for a rare event). Worst case, what happens if the user presses five keys or as many as he/she can? Should your code be able to handle that too?
On my Mac, I seem to be only able to "record" five key presses at once. So I can keep five keys pressed (all five register key press events) but the sixth, seventh, etc. key press does not register. So there may be a maximum number of key press events that can be managed at any one time.
its ok i want record of four key presses.can you help me
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.

Sign in to comment.

More Answers (1)

i dint able to do with multiple key.So i tried doing it by taking some fixed keys.actualy i am not getting how to keep track of key using evnt.key.
function keypress_Test
h = figure;
set(h,'KeyPressFcn',@keyDownCb,'KeyReleaseFcn',@keyUpCb);
function keyDownCb(src,evnt)
if strcmp(evnt.key,'p')
flag==0;
else
return;
end
end
tic;
function keyUpCb(src,evnt)
global ticStartTime;
flag==1;
end
function keyDownCb(src,evnt)
if strcmp (evnt.key,'u')
time=toc(ticStartTime);
end
disp(time)
ticStartTime=tic;
end
end
but its not working.its showing error as because i am using keydown callback two times.

1 Comment

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.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!