# "Customize keyboard shortcuts" from script repository not working?

**URL:** <https://discourse.slicer.org/t/customize-keyboard-shortcuts-from-script-repository-not-working/11906>\
**Category:** Support\
**Created:** [June 6, 2020, 7:12pm UTC](https://discourse.slicer.org/t/customize-keyboard-shortcuts-from-script-repository-not-working/11906 "2020-06-06T19:12:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hherhold](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hherhold/32/12199_2.png) [@hherhold](https://discourse.slicer.org/u/hherhold)\
**Post date:** [June 6, 2020, 7:12pm UTC](https://discourse.slicer.org/t/customize-keyboard-shortcuts-from-script-repository-not-working/11906/1 "2020-06-06T19:12:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hherhold](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hherhold/32/12199_2.png) [@hherhold](https://discourse.slicer.org/u/hherhold)\
**Post date:** [June 6, 2020, 7:13pm UTC](https://discourse.slicer.org/t/customize-keyboard-shortcuts-from-script-repository-not-working/11906/2 "2020-06-06T19:13:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 6, 2020, 7:29pm UTC](https://discourse.slicer.org/t/customize-keyboard-shortcuts-from-script-repository-not-working/11906/3 "2020-06-06T19:29:29Z")

</div>

Can you post the code?

---

<div class="post-metadata">

**Author:** ![hherhold](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hherhold/32/12199_2.png) [@hherhold](https://discourse.slicer.org/u/hherhold)\
**Post date:** [June 6, 2020, 7:32pm UTC](https://discourse.slicer.org/t/customize-keyboard-shortcuts-from-script-repository-not-working/11906/4 "2020-06-06T19:32:05Z")

</div>

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)
```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 6, 2020, 7:37pm UTC](https://discourse.slicer.org/t/customize-keyboard-shortcuts-from-script-repository-not-working/11906/5 "2020-06-06T19:37:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hherhold](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hherhold/32/12199_2.png) [@hherhold](https://discourse.slicer.org/u/hherhold)\
**Post date:** [June 6, 2020, 7:39pm UTC](https://discourse.slicer.org/t/customize-keyboard-shortcuts-from-script-repository-not-working/11906/6 "2020-06-06T19:39:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 6, 2020, 7:43pm UTC](https://discourse.slicer.org/t/customize-keyboard-shortcuts-from-script-repository-not-working/11906/7 "2020-06-06T19:43:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hherhold](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hherhold/32/12199_2.png) [@hherhold](https://discourse.slicer.org/u/hherhold)\
**Post date:** [June 6, 2020, 7:49pm UTC](https://discourse.slicer.org/t/customize-keyboard-shortcuts-from-script-repository-not-working/11906/8 "2020-06-06T19:49:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 6, 2020, 8:00pm UTC](https://discourse.slicer.org/t/customize-keyboard-shortcuts-from-script-repository-not-working/11906/9 "2020-06-06T20:00:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hherhold](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hherhold/32/12199_2.png) [@hherhold](https://discourse.slicer.org/u/hherhold)\
**Post date:** [June 6, 2020, 8:11pm UTC](https://discourse.slicer.org/t/customize-keyboard-shortcuts-from-script-repository-not-working/11906/10 "2020-06-06T20:11:33Z")

</div>

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

Thanks!!!
