1

I'm building a GUI in MATLAB and currently adding custom menus using uimenu. I'm trying to add different accelerators to different menu actions.

I've found that passing char(10) (newline) as the accelerator character in uimenu (see below) matlab adds Ctrl+ Enter as that menu's accelerator label. The problem is that it will not run the callback when I hit Ctrl+ Enter.

Any ideas why this doesn't work? Am I missing something? Is the Ctrl+ Enter for "run current section" cancelling my call? In that case, can I override it?

Example

A quick demonstrative example of how MATLAB will not accept Ctrl+ Enter

function test
close all
f=figure;
m=uimenu(f,'Label','test');
uimenu(m,'Label','a','callback',@hittest,'Accelerator','r');
uimenu(m,'Label','b','callback',@hittest,'Accelerator',char(10));

    function hittest(h,~)
        disp(h.Label)
    end
end
Skogsv
  • 494
  • 3
  • 14
  • Well your callback errors out but this works for me on OS X. Can you try running MATLAB without the desktop to confirm what you suspect? Also what version and OS? – Suever Aug 01 '16 at 15:12
  • Version: 9.0.0.370719 (R2016a) Operating System: Microsoft Windows 7 Enterprise Version 6.1 (Build 7601: Service Pack 1) I will try to run windowless and come back! – Skogsv Aug 01 '16 at 15:20
  • Ok it works for me too as long as I run it in `-nodesktop` mode. I guess that's fine as it will be deployed when I'm done with it. Any Idea if it is possible to make it work even in desktop mode? – Skogsv Aug 01 '16 at 15:27
  • I suspect that there isn't a good way to do this in desktop mode. It appears that the application hijacks that shortcut at a very low level. – Suever Aug 01 '16 at 15:29
  • Ok, too bad. I'm still very new to stackexchange, what is proper action here? To remove my post or answer myself for others to see in the future? – Skogsv Aug 01 '16 at 15:32
  • No I think it's good to have. I've added a more detailed description of what is happening below as an answer and a possible workaround for developing without having to start in headless mode. – Suever Aug 01 '16 at 15:33
  • Can you possibly change it in the keyboard shortcut preferences panel? – Suever Aug 01 '16 at 15:38

1 Answers1

1

As you've stated, it appears the main application has registered this accelerator and is therefore preventing your GUI from intercepting this call.

You could try to change MATLAB's keyboard shortcut in the shortcut preferences dialog. Note that this will only affect your installation of MATLAB.

If you start MATLAB in -nodesktop mode, then this will prevent the MATLAB IDE from launching the IDE and should free up the accelerator for your use.

matlab -nodesktop

Since you mention that this will be a deployed application, you could always use isdeployed to check if it is being run as a deployed application and if it's not then you could use an alternate keyboard shortcut so you don't have to continuously start MATLAB without an IDE

if ~isdeployed
    % Use some other keyboard shortcut for testing
    set(hmenu, 'Accelerator', <some other key for testing>)
else
    % Use the enter key on deployed applications
    set(hmenu, 'Accelerator', char(10))
end

You could also make it so that any time your app is deployed or matlab is being run with -nodesktop it would use the enter key:

if usejava('desktop')
    % Use some other keyboard shortcut for testing
    set(hmenu, 'Accelerator', <some other key for testing>)
else
    % Use the enter key on deployed applications
    set(hmenu, 'Accelerator', char(10))
end
Suever
  • 64,497
  • 14
  • 82
  • 101