Parallel Problem: Get keyboard input in 60 sec, otherwise jump out
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi to all, I recently encountered a problem. I want to get the keyboard input and read it and put something on the screen if the user puts a cerntain key such as 'q'. However, if the user doesn't put in anything in 20 sec, I want the time counter to start again. This procedure loops over for 60 times.
Can anyone help me with this? Thank you very much!
0 commentaires
Réponse acceptée
Sven
le 19 Fév 2012
Shijia, you can do something like this using callbacks. Here's an example using figure callbacks. Save it as myFunction.m and see if you can follow what it does:
function myFunction
figure('KeyPressFcn',@kp);
ticCount = 0;
lastTic = 0;
startTimer()
function startTimer()
ticCount = ticCount+1;
if ticCount>10
fprintf('We''ve already reached the end!!!!\n')
return;
end
lastTic = tic;
fprintf('Starting the timer for time number %d\n',ticCount)
justWait(5)
end
function kp(~,evnt)
fprintf('User pressed %s\n',evnt.Character)
if evnt.Character == 'q'
fprintf('It took the user %g seconds...\n', toc(lastTic))
startTimer()
end
end
function justWait(len)
oldTicCount = ticCount;
fprintf('About to wait for %g seconds...\n',len)
pause(len)
if ticCount==oldTicCount
fprintf('Nothing happened for %g seconds...\n',len)
startTimer()
end
end
end
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Entering Commands dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!