Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
java: add keyListener to command window

Subject: java: add keyListener to command window

From: Mola Ram

Date: 5 Aug, 2009 16:49:02

Message: 1 of 4

Hi,
 
i'm trying to interrupt a loop via command window key press. The loop is running and when the user presses a key the execution should stop. I started to solve this using java but now i got stuck (i'm new to java).
Here the code:

% get command window handle
h_cw = com.mathworks.mde.desk.MLDesktop.getInstance;
h_cw = mde.getClient('Command Window')

% focus command window
h_cw.requestFocus

%add keyListener
h_cw.addkeyListener(java.awt.KeyListener.keyPressed(java.awt.event.KeyEvent.VK_ENTER)) <---doesn't work

I tried several things but none of it worked.
How can i add a KeyListener? Or is there a much simpler solution?

Greets
Mola

Subject: java: add keyListener to command window

From: Yair Altman

Date: 5 Aug, 2009 21:29:04

Message: 2 of 4

"Mola Ram" <MolaRam@web.de> wrote ...
> Hi,
>
> i'm trying to interrupt a loop via command window key press. The loop is running and when the user presses a key the execution should stop. I started to solve this using java but now i got stuck (i'm new to java).
> Here the code:
>
> % get command window handle
> h_cw = com.mathworks.mde.desk.MLDesktop.getInstance;
> h_cw = mde.getClient('Command Window')
>
> % focus command window
> h_cw.requestFocus
>
> %add keyListener
> h_cw.addkeyListener(java.awt.KeyListener.keyPressed(java.awt.event.KeyEvent.VK_ENTER)) <---doesn't work
>
> I tried several things but none of it worked.
> How can i add a KeyListener? Or is there a much simpler solution?
>
> Greets
> Mola

You were fairly close:

mde = com.mathworks.mde.desk.MLDesktop.getInstance;
cw = mde.getClient('Command Window');
xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);
h_cw = handle(xCmdWndView,'CallbackProperties');
set(h_cw, 'KeyPressedCallback', @MyMatlabFunction);
set(h_cw, 'KeyPressedCallback', {@MyMatlabFunction,extraParam1,...}); %alternative

Inside your callback function MyMatlabFunction, which accepts the caller reference, the eventData reference and your extraParams [if any], you should check eventData to detect which key was pressed and act accordingly.

A related article: http://undocumentedmatlab.com/blog/uicontrol-callbacks/

Yair Altman
http://UndocumentedMatlab.com

Subject: java: add keyListener to command window

From: Mola Ram

Date: 6 Aug, 2009 08:39:01

Message: 3 of 4

Hi Yair,

thanks for your help i could solve my problem.
But there are two questions left:

 What do you achieve with 'xCmdWndView = cw.getComponent (0).getViewport.getComponent(0)' ?
I thougt 'cw = mde.getClient('Command Window')' is the handle to command window.

 How do i reset the callback, with 'set(h_cw,'KeyPressedCallback',' ')'?

Greets
Mola

Subject: java: add keyListener to command window

From: Yair Altman

Date: 6 Aug, 2009 09:01:19

Message: 4 of 4

> What do you achieve with 'xCmdWndView = cw.getComponent (0).getViewport.getComponent(0)' ?
> I thougt 'h_cw = mde.getClient('Command Window')' is the handle to command window.

this h_cw reference is only a reference to the container of the Command Window (including the scrollbars etc.), so setting the callbacks on it doesn't work. You need to set it on the actual edittable pane, which is xCmdWndView

> How do i reset the callback, with 'set(h_cw,'KeyPressedCallback',' ')'?

Yes

Yair Altman
http://UndocumentedMatlab.com
 

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us