# How to set up shortcuts from module on startup?

**URL:** <https://discourse.slicer.org/t/how-to-set-up-shortcuts-from-module-on-startup/7982>\
**Category:** Development\
**Created:** [August 11, 2019, 11:41pm UTC](https://discourse.slicer.org/t/how-to-set-up-shortcuts-from-module-on-startup/7982 "2019-08-11T23:41:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amine](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/amine/32/4844_2.png) [@Amine](https://discourse.slicer.org/u/Amine)\
**Post date:** [August 11, 2019, 11:41pm UTC](https://discourse.slicer.org/t/how-to-set-up-shortcuts-from-module-on-startup/7982/1 "2019-08-11T23:41:12Z")

</div>

Hi, to register keyboard shortcuts from a scripted module i have been putting the code in the setup method of my widget class, however there is a need to run each module to register its shortcuts, where should shortcut registration be placed in a module so it can run as soon as slicer starts?  
I tried setting it as a separate method in widget class then call it but then i get "Failed to obtain reference to ‘qSlicerMainWindow’ " error

```
def Register_Shortcuts():
    def funct():
        print("OKAY")
    shortcuts = [
        ('x', funct),
        ('b', funct)]
    for (shortcutKey, callback) in shortcuts:
        shortcut = qt.QShortcut(slicer.util.mainWindow())
        shortcut.setKey(qt.QKeySequence(shortcutKey))
        shortcut.connect('activated()', callback)
Register_Shortcuts()
```

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 12, 2019, 12:15pm UTC](https://discourse.slicer.org/t/how-to-set-up-shortcuts-from-module-on-startup/7982/2 "2019-08-12T12:15:26Z")

</div>

Hi -

A couple options - if it’s just for the user you can put the code [in ~/.slicerrc.py](https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/slicerqt.py#L103-L127). Or if you want this to be part of the module you can use [the startupComplete() signal from the application](https://github.com/Slicer/Slicer/blob/master/Modules/Scripted/DICOM/DICOM.py#L43) to trigger the code.

---

<div class="post-metadata">

**Author:** ![Amine](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/amine/32/4844_2.png) [@Amine](https://discourse.slicer.org/u/Amine)\
**Post date:** [August 12, 2019, 3:45pm UTC](https://discourse.slicer.org/t/how-to-set-up-shortcuts-from-module-on-startup/7982/3 "2019-08-12T15:45:56Z")

</div>

Thanks a lot.  
Indeed slicerrc needs to be edited which is not ideal for module sharing.  
startupComplete() signal is exactly what i was looking for. Thanks a lot for your help.

Note to interested people: i had to put in the **init** method of the main module class to be run at slicer’s startup

---

<div class="post-metadata">

**Author:** ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)\
**Post date:** [August 12, 2019, 9:32pm UTC](https://discourse.slicer.org/t/how-to-set-up-shortcuts-from-module-on-startup/7982/4 "2019-08-12T21:32:00Z")

</div>

If there is a risk of shortcuts conflicting between modules, and you only want to use specific shortcuts in your module, you could have a look at this example (search for “shortcut” in the code). It installs shortcuts when you enter the module, but uninstalls them when you switch to a different module.

> <https://github.com/SlicerIGT/UsAnnotationExport/blob/master/SlicerExtension/UsAnnotationExport/SingleSliceSegmentation/SingleSliceSegmentation.py>

---

<div class="post-metadata">

**Author:** ![Amine](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/amine/32/4844_2.png) [@Amine](https://discourse.slicer.org/u/Amine)\
**Post date:** [August 13, 2019, 8:42pm UTC](https://discourse.slicer.org/t/how-to-set-up-shortcuts-from-module-on-startup/7982/5 "2019-08-13T20:42:50Z")

</div>

Thanks for the precision, that will definitely come in handy,  
i don’t see how enter and exit methods (lines 317 and 375) get called? do slicer recognize their names and runs them automatically like the setup method?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 13, 2019, 8:53pm UTC](https://discourse.slicer.org/t/how-to-set-up-shortcuts-from-module-on-startup/7982/6 "2019-08-13T20:53:51Z")

</div>

> [@Amine](#):
>
> i don’t see how enter and exit methods (lines 317 and 375) get called? do slicer recognize their names and runs them automatically like the setup method?

yes, it does automatically call them

---

<div class="post-metadata">

**Author:** ![Amine](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/amine/32/4844_2.png) [@Amine](https://discourse.slicer.org/u/Amine)\
**Post date:** [August 13, 2019, 9:04pm UTC](https://discourse.slicer.org/t/how-to-set-up-shortcuts-from-module-on-startup/7982/7 "2019-08-13T21:04:17Z")

</div>

Quite useful, Thanks for your help!

---

<div class="post-metadata">

**Author:** ![dalv.silvermann](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dalv.silvermann/32/16193_2.png) [@dalv.silvermann](https://discourse.slicer.org/u/dalv.silvermann)\
**Post date:** [August 7, 2022, 7:19pm UTC](https://discourse.slicer.org/t/how-to-set-up-shortcuts-from-module-on-startup/7982/8 "2022-08-07T19:19:19Z")

</div>

Dear Tamas,  
Could you please reload the link?

---

<div class="post-metadata">

**Author:** ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)\
**Post date:** [August 7, 2022, 7:50pm UTC](https://discourse.slicer.org/t/how-to-set-up-shortcuts-from-module-on-startup/7982/9 "2022-08-07T19:50:34Z")

</div>

It’s better to take examples from the official script repository: [Script repository — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#keyboard-shortcuts-and-mouse-gestures)

But here it is:

> <https://github.com/SlicerIGT/aigt/blob/30bbb67ecbfdd6bc27a954803cbd211829814478/SlicerExtension/LiveUltrasoundAi/SingleSliceSegmentation/SingleSliceSegmentation.py#L75>
