Create keyboard listener to interrupt a running function
30 views (last 30 days)
Show older comments
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 !
0 Comments
Accepted Answer
Jos (10584)
on 21 Oct 2016
You might be interested in my File Exchange contribution STOPLOOP or GETKEY: https://uk.mathworks.com/matlabcentral/fileexchange/20455-stoploop--v1-0--jun-2008- https://uk.mathworks.com/matlabcentral/fileexchange/7465-getkey
2 Comments
More Answers (1)
Jan
on 24 Oct 2016
Edited: Jan
on 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 Comments
JohnGalt
on 15 Jun 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
on 19 Jun 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.
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!