"Customize keyboard shortcuts" from script repository not working?

Okay, I’m positive I’m doing something wrong here, but I can’t figure out what it is. I’m trying to make a new keyboard shortcut based on the code from the script repository, but I can’t even get the example working. The code is loading and running fine from my .slicerrc.py but it is not working.

Oops, hit post to quickly.

I get a “True” back when connecting the signal/slot, so everything appears happy, but the event never goes.

Any ideas? Thanks!!

-Hollister

Can you post the code?

Sure, it’s straight from the script repository.

shortcuts = [
  ('Ctrl+b', lambda: slicer.app.layoutManager().setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutOneUpRedSliceView)),
  ('Ctrl+n', lambda: slicer.app.layoutManager().setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutOneUpYellowSliceView)),
  ('Ctrl+m', lambda: slicer.app.layoutManager().setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutOneUpGreenSliceView)),
  ('Ctrl+,', lambda: slicer.app.layoutManager().setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpView))
  ]

for (shortcutKey, callback) in shortcuts:
    shortcut = qt.QShortcut(slicer.util.mainWindow())
    shortcut.setKey(qt.QKeySequence(shortcutKey))
    shortcut.connect( 'activated()', callback)

This works well for me on recent Slicer-4.11 on Windows. Maybe there is something wrong with your .slicerrc.py file, so try to copy-paste it into the Python console.

Hmm. Wonder if it’s a Mac thing. I did exactly that (copy paste into python console) and it doesn’t work.

I’ll try other key combos using actual Qt_Key:: values.

On Mac, probably you need to write “Cmd” instead of “Ctrl”.

Okay, this is odd.

You’re right on the mark with Cmd vs Ctrl. Cmd plus those keys works. But if I change the key text to ‘Cmd+b’, for example, nothing works.

So basically Ctrl is Cmd on a Mac for this. Is this a PyQt thing? In my experience Qt is pretty good at this type of cross-platform thing, but I’ve only ever done Qt programming from C++.

Thanks!!

-Hollister

It is a Macintosh keyboard thing that Ctrl is called Cmd. The best that Qt can do about this is to abstract it away:

Note: On Mac OS X, references to “Ctrl”, Qt::CTRL, Qt::Control and Qt::ControlModifier correspond to the Command keys on the Macintosh keyboard, and references to “Meta”, Qt::META, Qt::Meta and Qt::MetaModifier correspond to the Control keys. Developers on Mac OS X can use the same shortcut descriptions across all platforms, and their applications will automatically work as expected on Mac OS X.

Ugh. Annoying. And Meta should be Meta, as in the old LISP machines and Sun keyboards. Oh well.

Thanks!!!