Maya shortcut toggle
-
Hi everybody. I'm trying to create a shortcut that acts as a toggle so I want the KeyPress and KeyRelease to be implemented and run two opposite commands. In this case opening and closing the shelf.
I've tried a few different ways, without any luck. Tried making my own class, inherited from QtWidgets.QShortcut and implementing eventfilter, but seems I don't know what I'm doing.
For reference, this code works, but it doesn't toggle the way I want.
import pymel.core as pm import maya.mel as mel from functools import partial from maya import OpenMayaUI as omui from PySide2 import QtWidgets, QtGui, QtCore from shiboken2 import wrapInstance def getMainMayaWindow(): mayaMainWindowPtr = omui.MQtUtil.mainWindow() mayaMainWindow = wrapInstance(int(mayaMainWindowPtr), QWidget) return mayaMainWindow def shelfHeightToggle(): gShelfTopLevel = mel.eval('$tmpVar=$gShelfTopLevel') value = pm.shelfTabLayout(gShelfTopLevel, q=1, h=1) if value == 64: value = 94 else: value = 64 pm.shelfTabLayout(gShelfTopLevel, e=1, h=value) shelfHeightShortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_section), getMainMayaWindow()) shelfHeightShortcut.setContext(QtCore.Qt.ApplicationShortcut) shelfHeightShortcut.activated.connect(partial(shelfHeightToggle)) -
Hi and welcome to devnet,
Why not use a toggle action as target of your shortcut ?
-
@SGaist Thank you. I'm not at all familiar with a toggle action so I will see what I can read about it.
With that said. I managed to get it working with creating a class with an event filter method with KeyPress and KeyRelease. But I'm very new so I don't know anything about the best practices. -
The checked property is what you are looking for.