Effacer les filtres
Effacer les filtres

Create keyboard listener to interrupt a running function

34 vues (au cours des 30 derniers jours)
Mathieu
Mathieu le 21 Oct 2016
Commenté : Jan le 19 Juin 2017
Hello,
I am working on a project in which I have to interrupt a running function if the user press the Escape button of the keyboard. I explain : the running function is in two parts and between them there is (for now) a pause of 5 seconds. But, what I want is replace my pause by a while function which end after 5 seconds or if the user press the escape button (and in this case I want to end the running function to not execute second part of the code).
I saw it deals with listener but I don't understand how to implement this without create a figure which disturb me because I have no GUI open.
Can you help me please to understand how to solve my problem? I thank you in advance !

Réponse acceptée

Jos (10584)
Jos (10584) le 21 Oct 2016
  2 commentaires
Mathieu
Mathieu le 21 Oct 2016
Thanks for your answer but these solutions use a Window or message box which is the same. So the problem remains because something appears on the screen and can disturb the user.
Jos (10584)
Jos (10584) le 24 Oct 2016
What is being shown on the screen in your case?

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 24 Oct 2016
Modifié(e) : Jan le 26 Oct 2016
If the user has a chance to interrupt the program, a message is not confusing but useful. Therefore I'd prefer the solution using a figure with a meaningful message.
You could catch a keypress event in tme command window also, but this works only, if it is currently the active window. So you still have the problem of an open and active dialog window.
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
cw = desktop.getClient('Command Window'); $ Or desktop.getMainFrame?
xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);
h_cw = handle(xCmdWndView,'CallbackProperties');
set(h_cw, 'KeyPressedCallback', @CmdKeyCallback);
As usual this code is based on information provided by Yair. Search for "undocumented Matlab" in the net for more details.
Now you could store e.g. a persistent variable inside the function to check for a KeyPress
function Value = CmdKeyCallback(ObjectH, EventData)
persistent KeyPresssed
switch nargin
case 0
Value = ~isempty(KeyPressed);
case 1
KeyPressed = [];
case 2
KeyPressed = true;
otherwise
error('Programming error');
end
end
Then you can create the loop:
CmdKeyCallback('reset');
ini = cputime;
while cputime - ini < 5
pause(0.02);
if CmdKeyCallback()
disp('Key pressed');
break;
end
end
Please test and debug this, it is written in the forums interface only.
  2 commentaires
JohnGalt
JohnGalt le 15 Juin 2017
thanks for this - it's almost what i'm looking for... I want to interrupt running code if I press 'Esc' so with slight modification your code becomes:
function keyboard_interrupt_test()
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
cw = desktop.getClient('Command Window');
xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);
h_cw = handle(xCmdWndView,'CallbackProperties');
set(h_cw, 'KeyPressedCallback', @CmdWinKeyCallback);
num = 1;
CmdWinKeyCallback('reset');
while true
pause(0)
if CmdWinKeyCallback()
disp(['Key pressed ' num2str(num)]);
disp('type ''dbcont'' to continue')
keyboard
CmdWinKeyCallback('reset');
disp('resuming')
num = num+1;
end
end
end
function Value = CmdWinKeyCallback(ObjectH, EventData)
persistent KeyPressed
switch nargin
case 0
Value = ~isempty(KeyPressed);
case 1
KeyPressed = [];
case 2
if get(EventData,'keyCode')==27 % 27 = 'Esc'
KeyPressed = true;
else
KeyPressed = [];
end
otherwise
error('Programming error');
end
end
The issue now is that the keypresses are queued up and are issued to the command window when the code stops when I press 'Esc'... Any idea how to deal with that? (also - can you send a link to the Undocumented Matlab source please - I didn't manage to find the article) thanks
Jan
Jan le 19 Juin 2017
There was no article about setting the KeyPressedCallback of the command window, but I've learned many other details from Yair. Do you really want to use the command window as a GUI. Opening a dedicated figure migth be easier and nicer.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Desktop dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by