# Making keyboard shortcuts to open modules

**URL:** <https://discourse.slicer.org/t/making-keyboard-shortcuts-to-open-modules/21375>\
**Category:** Support\
**Tags:** python\
**Created:** [January 10, 2022, 5:05am UTC](https://discourse.slicer.org/t/making-keyboard-shortcuts-to-open-modules/21375 "2022-01-10T05:05:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hourglassnam](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hourglassnam/32/11796_2.png) [@hourglassnam](https://discourse.slicer.org/u/hourglassnam)\
**Post date:** [January 10, 2022, 5:05am UTC](https://discourse.slicer.org/t/making-keyboard-shortcuts-to-open-modules/21375/1 "2022-01-10T05:05:28Z")

</div>

Hi everyone!  
I have been writing a python script that allows opening specific modules using keyboard shortcuts.  
However, the script does not work quite right.  
Below is what I could put together based on the forum topics.

> #Makes selected module to popup as second module  
> def openSegmentEditor():  
> slicer.modules.segmenteditor.widgetRepresentation().setParent(None)  
> slicer.modules.segmenteditor.widgetRepresentation().show()
> 
> #setup the short cut  
> shortcut1 = qt.QShortcut(slicer.util.mainWindow())  
> shortcut1.setKey(qt.QKeySequence(“Ctrl+n”))  
> shortcut1.connect( ‘activated()’, openSegmentEditor)

This script allowed me to open the segment module as a second panel using ctrl+n.  
However, the SegmentEditor hotkeys(numbers, shit+numbers) do not work when opened as a second module.

I am new to python, and I have been looking all over the forum but could not find the proper solution to this problem.  
I would be grateful if anyone could help me out with this.

So my questions are:

1. Is there any way to open the module on the main panel?
2. I am currently planning to make a shortcut that would allow me to not only open the module but also lead me to the exact widget. For instance, I want to create a shortcut that would directly lead me to EraseInside of the Scissor from the main window. Would this be better, or should I just set up a toggle hotkey and switch around?

Thank you always for sharing your knowledge!

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [January 10, 2022, 7:43am UTC](https://discourse.slicer.org/t/making-keyboard-shortcuts-to-open-modules/21375/2 "2022-01-10T07:43:17Z")

</div>

> [@hourglassnam](#):
>
> Is there any way to open the module on the main panel?

Try this :

```auto
#Makes selected module to popup as second module
def openSegmentEditor():
  mainWindow = slicer.util.mainWindow()
  mainWindow.moduleSelector().selectModule('SegmentEditor')

  
  
#setup the short cut
shortcut1 = qt.QShortcut(slicer.util.mainWindow())
shortcut1.setKey(qt.QKeySequence('Ctrl+n'))
shortcut1.connect('activated()', lambda: openSegmentEditor())

```

Use single quotes only : `‘activated()’ -> 'activated()'`

---

<div class="post-metadata">

**Author:** ![hourglassnam](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hourglassnam/32/11796_2.png) [@hourglassnam](https://discourse.slicer.org/u/hourglassnam)\
**Post date:** [January 10, 2022, 8:04am UTC](https://discourse.slicer.org/t/making-keyboard-shortcuts-to-open-modules/21375/4 "2022-01-10T08:04:04Z")

</div>

That worked beautifully! Thank you so much!!

---

<div class="post-metadata">

**Author:** ![hourglassnam](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hourglassnam/32/11796_2.png) [@hourglassnam](https://discourse.slicer.org/u/hourglassnam)\
**Post date:** [January 10, 2022, 8:36am UTC](https://discourse.slicer.org/t/making-keyboard-shortcuts-to-open-modules/21375/5 "2022-01-10T08:36:14Z")

</div>

> [@chir.set](#):
>
> Use single quotes only : `‘activated()’ -> 'activated()'`

May I ask one additional question?  
[https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository.html](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html)  
In here, they used “activated()”.  
May I ask why I should only use single quote?

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [January 10, 2022, 8:45am UTC](https://discourse.slicer.org/t/making-keyboard-shortcuts-to-open-modules/21375/6 "2022-01-10T08:45:04Z")

</div>

> [@hourglassnam](#):
>
> May I ask why I should only use single quote?

In your first post, the ‘activated()’ signal is not between single quotes, but other similarly looking characters, at least the second ‘quote but not a quote’. By copy/paste in the python console, it won’t run. I did not try with double quotes, may be it’s OK. I meant : not other characters that look like single quote.

---

<div class="post-metadata">

**Author:** ![hourglassnam](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hourglassnam/32/11796_2.png) [@hourglassnam](https://discourse.slicer.org/u/hourglassnam)\
**Post date:** [January 10, 2022, 9:02am UTC](https://discourse.slicer.org/t/making-keyboard-shortcuts-to-open-modules/21375/7 "2022-01-10T09:02:48Z")

</div>

> [@chir.set](#):
>
> In your first post, the ‘activated()’ signal is not between single quotes, but other similarly looking characters, at least the second ‘quote but not a quote’. By copy/paste in the python console, it won’t run. I did not try with double quotes, may be it’s OK. I meant : not other characters that look like single quote.

Aha! I understand now! Thank you for your reply!

---

<div class="post-metadata">

**Author:** ![DIV](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/div/32/12816_2.png) [@DIV](https://discourse.slicer.org/u/DIV)\
**Post date:** [January 18, 2022, 5:30am UTC](https://discourse.slicer.org/t/making-keyboard-shortcuts-to-open-modules/21375/8 "2022-01-18T05:30:10Z")

</div>

The quotation issue may be an “auto-correct”/rendering issue with the discourse forum, for text outside of ‘code’ demarcation boundaries…

```auto
# [...] an "auto-correct" issue with the discourse forum, for text outside of 'code' demarcation boundaries....
dummy = "text"

```

**chir.set** is right to warn about this, though, as it can lead to unexpected problems!

> [@Segment Editor: reduce the number of mouse clicks to set basic colour for segment](https://discourse.slicer.org/t/segment-editor-reduce-the-number-of-mouse-clicks-to-set-basic-colour-for-segment/20236/7):
>
> Hello, James. My username is purely alphanumeric; besides that, the file is exactly where I said: C:/Users/[username]/.slicerrc.py. thinking… working… OK, I have found the cause of the error message. I had inserted the following into the start-up script file (on the last three lines): # Always use the “Basic” color picker # https://discourse.slicer.org/t/segment-editor-reduce-the-number-of-mouse-clicks-to-set-basic-colour-for-segment/20236/3 ctk.ctkColorDialog().setDefaultTab(0) If you…

—DIV
