1
function interrupt

import java.awt.event.KeyEvent
import java.lang.reflection.*

base = com.mathworks.mde.cmdwin.CmdWin.getInstance();
hCmd = base.getComponent(0).getViewport().getView();
cmdwin = handle(hCmd,'CallbackProperties');

argSig = javaArray('java.lang.Class',1);
argSig(1) = java.lang.Class.forName('java.awt.event.KeyEvent');

msTime = (8.64e7 * (now - datenum('1970', 'yyyy')));
args = javaArray('java.lang.Object',1);
args(1) = KeyEvent(cmdwin,KeyEvent.KEY_PRESSED,msTime,...
    KeyEvent.CTRL_DOWN_MASK,KeyEvent.VK_C,KeyEvent.CHAR_UNDEFINED);

method = cmdwin.getClass().getDeclaredMethod('processKeyEvent',argSig);
method.setAccessible(true);
method.invoke(cmdwin,args);

The above code was pasted from this answer. I just need to understand or find API/Documentation about this line:

com.mathworks.mde.cmdwin.CmdWin.getInstance();

I saw similar stuff all over the internet. What is it and where can I find any source?

Community
  • 1
  • 1
SimonMatt
  • 53
  • 1
  • 4
  • 3
    It gets the Java handle to MATLAB's command window. MATLAB's underlying Java is not documented. – sco1 Aug 02 '16 at 20:20

1 Answers1

1

This is an unsupported and undocumented API to access the command window. More examples of its use can be found at Undocumented Matlab. You're accessing directly the Java components that MATLAB is built with, so it's best not to rely on these things to be stable or long-lived.

miken32
  • 42,008
  • 16
  • 111
  • 154
  • Thanks, for the info. I am trying to find a way to automatically execute/code Ctrl+C. I looked at everything and I am out of luck. So I decided to look into Java stuff. It works, but I want to do something that I understand.Can you suggest something? – SimonMatt Aug 02 '16 at 20:32
  • @SimonMatt Have you looked into the `java.awt.Robot` class? A similar example can be found here: http://stackoverflow.com/questions/27933270/programmatically-press-an-enter-key-after-starting-exe-file-in-matlab - Modify for your own purposes. – rayryeng Aug 02 '16 at 20:55
  • Doesn't solve my problem using Robot. However, solution from @MattB works. But that solution is very hard to understand. – SimonMatt Aug 03 '16 at 14:12