Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab control+enter key on figure

I want to capture when the user holds down the control key and presses the enter key on a figure window. Note: This is the default keys for "Evaluate Current Section" in the editor.

See example code below:

function test
  f = figure;  
  f.KeyPressFcn = @myKeyPressFcn;
end
function myKeyPressFcn ( hFig, event )
  cm = hFig.CurrentModifier();  
  if ~isempty ( cm )
    fprintf ( 'CurrentKey: %s:%s\n', cm{1}, hFig.CurrentKey );
  else 
    fprintf ( 'CurrentKey: %s\n', hFig.CurrentKey );
  end
end

To reproduce save the above in an active file in the editor and run the function - the editor needs to be open (this is important!!).

With the figure active press any key -> the selected key is written to the terminal window. However if you hold down Control and press the enter (return) key then this is not captured but instead Matlab attempts to "Evaluate Current Section" (or cell as it used to be called) in the editor (no matter that the figure has the focus). This of course throws as error...

I have contacted Matlab support and this is "expected behaviour!". I can (just about) see why it might be a good idea for demos - but for professional applications that run in Matlab I personally think this "feature" is a bug!!

Notes

  1. When the editor is closed the control+enter is captured in the figure
  2. In deployed applications the control+enter is captured.
  3. If I manually change the Evaluate Current Section shortcut then control+enter is captured.

I have tried a number of things to resolve this but none have worked, for example hiding the editor or setting editor enable state to false (neither of these are acceptable solutions - I was trying to see what I could get to work on a small test case...):

function test
  desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
  jEditor = desktop.getGroupContainer('Editor').getTopLevelAncestor;
  jEditor.setVisible(false);
  jEditor.setEnable(false);
  f = figure
  f.KeyPressFcn = @myKeyPressFcn;
  uiwait(f);
  jEditor.setVisible(true);
  jEditor.setEnable(true);
end

The only way I can get it to work is to close all of the editor files on launching the GUI and then opening them again when the GUI closes (this is not an acceptable solution... - for fairly obvious reasons!!)

I did think about trying to temporarily modify the shortcut key (Preferences-Keyboard-Shortcuts) of the "Evaluate Current Section" -> but haven't worked out a way to do it from the commandline, and then set it back again when finished. If this is fast you could do it when the user presses and releases the control key.

So what am I asking:

If possible I need a solution that will work for anyone anywhere - as if I can get this to work it will be included in a new add-on feature in my Matlab GUI Toolbox. - which is used by people all over the world.

  1. Do you know how to modify the keyboard shortcuts from the commandline - if so how!
  2. Any other suggestions?
  3. My other idea is to change my shortcut to a different key combination - but wheres the fun in that! :) (I will still have the issue if some user somewhere has altered the execute the current cell to my new combination...)

P.S. Thanks for reading this far down!! :)

like image 444
matlabgui Avatar asked Jul 02 '15 12:07

matlabgui


1 Answers

Why don't you go to the home> Preferences > keyboard > Shortcutand change it there?

you only need to hit Ctrl + Enter in the black box at top of the page for searching the related command, which is here Evaluate Current Section and change it whatever you like.

Please bear in mind you will only need to split out your windows (Undock them). Then, when you click on Ctrl + Enter, it will do whatever you would like.

I hope you find this answer helpful.

like image 183
Iman Avatar answered Nov 09 '22 20:11

Iman