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

How to Restore Focus to Matlab after another process steals it

Asked by Jim Hokanson on 1 Jan 2013

I am launching a java process which unfortunately steals the focus away from Matlab. When typing in the command window this is really annoying. I've created some code which hides the launched process window, but I was wondering how I could pragmatically return focus to Matlab. Ideally there would be a single function which would restore focus to the last item with focus in Matlab but I am guessing this functionality doesn't exist. I have a working hack (see my answer) but I'd be interested in others solutions that are a bit more generic or that don't rely on undocumented features.

Thanks, Jim

0 Comments

Jim Hokanson

Products

No products are associated with this question.

1 Answer

Answer by Jim Hokanson on 1 Jan 2013
Accepted answer

Here's my solution, which only restores focus to the command window. Restoring focus to the command window is easy, the tricky part is knowing whether or not the focus belongs there.

%Step 1 - get focus status
cw = com.mathworks.mde.desk.MLDesktop.getInstance.getClient('Command Window');
cw_has_focus  = cw.hasFocus;
%Step 2 - launch process with focus stealer
%Step 3
if cw_has_focus
   commandwindow
end

Some things that got me on track: http://www.mathworks.com/matlabcentral/newsreader/view_thread/275241 http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/

0 Comments

Jim Hokanson

Contact us