# Shortcuts and keys already assigned somewhere else?

**URL:** <https://discourse.slicer.org/t/shortcuts-and-keys-already-assigned-somewhere-else/26632>\
**Category:** Development\
**Created:** [December 7, 2022, 5:11pm UTC](https://discourse.slicer.org/t/shortcuts-and-keys-already-assigned-somewhere-else/26632 "2022-12-07T17:11:00Z")\
**Posts on this page:** 5\
**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:** [December 7, 2022, 5:11pm UTC](https://discourse.slicer.org/t/shortcuts-and-keys-already-assigned-somewhere-else/26632/1 "2022-12-07T17:11:00Z")

</div>

Having odd issues with keyboard shortcuts (again). I have code set up like in the script repository:

```
shortcuts = [
  ('p', lambda: hh_select_paint()),
  ('[', lambda: hh_select_erase()),
  (']', lambda: hh_select_scissors()),
  ('\\', lambda: hh_select_islands()),
  (';', lambda: hh_place_fiducial()),
  ('s', lambda: toggleSphereBrush()),
  ('`', lambda: cycleEffect(1)),
  ('~', lambda: cycleEffect(-1)),
  ('Ctrl+m', lambda: slicer.util.selectModule('Models')),
  ('S', lambda: slicer.util.selectModule('SegmentEditor')),
  ('U', lambda: slicer.util.selectModule('Markups')),
  ('D', lambda: slicer.util.selectModule('Data')),
]

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

```

I’ve put debug prints in my various functions to do stuff (`toggleSphereBrush()`) and they don’t get executed, but interestingly, the backtick and tilde shortcuts _do_ work.

If I reassign backtick or tilde to my functions, my functions get called.

Are those other keys getting caught somewhere else?

THANKS!!

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [December 7, 2022, 5:29pm UTC](https://discourse.slicer.org/t/shortcuts-and-keys-already-assigned-somewhere-else/26632/2 "2022-12-07T17:29:02Z")

</div>

> [@hherhold](#):
>
> ```auto
> '`', lambda: cycleEffect(1)),
> ('~', lambda: cycleEffect(-1)),
> 
> ```

These are from SlicerMorphPrefs (Application Settings-\>Preferences-\>SlicerMorph). You may want to disable that first for your tests.

---

<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:** [December 7, 2022, 5:41pm UTC](https://discourse.slicer.org/t/shortcuts-and-keys-already-assigned-somewhere-else/26632/3 "2022-12-07T17:41:34Z")

</div>

Hey Murat! Hope you’re well.

Yep, that’s why I used them for testing - I know they work for SlicerMorph! Just wondering why the others don’t work.

Thanks!!

---

<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:** [December 7, 2022, 10:57pm UTC](https://discourse.slicer.org/t/shortcuts-and-keys-already-assigned-somewhere-else/26632/4 "2022-12-07T22:57:03Z")

</div>

Most of these shortcuts work for me (except the `S` and `s`, which you broke for yourself by redefining).

Try these:

```python
shortcuts = [
  ('p', lambda: print('pressed: p')),
  ('z', lambda: print('pressed: z')),
  ('q', lambda: print('pressed: q')),
  ('f', lambda: print('pressed: f')),
  ('[', lambda: print('pressed: [')),
  (']', lambda: print('pressed: ]')),
  ('\\', lambda: print('pressed: \\')),
  (';', lambda: print('pressed: ;')),
  ('`', lambda: print('pressed: `')),
  ('~', lambda: print('pressed: ~')),
  ('Ctrl+m', lambda: print('pressed: Ctrl-m')),
  ('S', lambda: print('pressed: S')),
  ('O', lambda: print('pressed: O')),
  ('U', lambda: print('pressed: U')),
  ('D', lambda: print('pressed: D')),
]

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

```

If you or someone else adds a shortcut with the same key sequence then effectively breaks all actions, as the shortcut will no longer emit `activated()` signal but `activatedAmbiguously()`.

Unfortunately, I don’t know an easy way to determine where each shortcut comes from where, other than search in the source code of all installed extensions for `shortcut`.

Extensions should not install any keyboard shortcut by default (without explicitly approved by the user). Also, if any shortcut is added then a meaningful `objectName` should be set, for example containing the name of the extension and module, to make it easier to detect and resolve conflicts by turning off irrelevant shortcuts.

---

<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:** [December 8, 2022, 12:54am UTC](https://discourse.slicer.org/t/shortcuts-and-keys-already-assigned-somewhere-else/26632/5 "2022-12-08T00:54:51Z")

</div>

Gaaaah, I should have realized ‘S’ and ‘s’ could be interpreted by Qt as the same keypress - I probably should have used `Qt::Shift` modifiers or whatever they are to be more explicit.

Thanks!
