필터 지우기
필터 지우기

Create keyboard listener to interrupt a running function

조회 수: 34 (최근 30일)
Mathieu
Mathieu 2016년 10월 21일
댓글: Jan 2017년 6월 19일
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 !

채택된 답변

Jos (10584)
Jos (10584) 2016년 10월 21일
  댓글 수: 2
Mathieu
Mathieu 2016년 10월 21일
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) 2016년 10월 24일
What is being shown on the screen in your case?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2016년 10월 24일
편집: Jan 2016년 10월 26일
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
JohnGalt
JohnGalt 2017년 6월 15일
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 2017년 6월 19일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Desktop에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by