Path: news.mathworks.com!not-for-mail
From: "Yair Altman" <altmanyDEL@gmailDEL.comDEL>
Newsgroups: comp.soft-sys.matlab
Subject: Re: java: add keyListener to command window
Date: Wed, 5 Aug 2009 21:29:04 +0000 (UTC)
Organization: TACT Computer Systems Ltd
Lines: 37
Message-ID: <h5ctj0$dqc$1@fred.mathworks.com>
References: <h5cd5u$pi5$1@fred.mathworks.com>
Reply-To: "Yair Altman" <altmanyDEL@gmailDEL.comDEL>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1249507744 14156 172.30.248.35 (5 Aug 2009 21:29:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 5 Aug 2009 21:29:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 642467
Xref: news.mathworks.com comp.soft-sys.matlab:561149

"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