Maya shelf button handle SHIFT click

Hi, dev,

Is there a way to catch SHIFT (or CTR) when clicking Shelf buttons in Maya?
E.g. click: print "A", SHIFT + click: print "B"

1 Like

The button works in a standalone application but It looks like maya is consuming the key event before the push button. Hopefully this will help in the right direction.

# Example of adding a button to the MASH menu.
import sys

from Qt import QtWidgets, QtCore

from maya import OpenMayaUI
import shiboken2 as shiboken


# Push button from: https://gist.github.com/justinfx/4197119
class Button(QtWidgets.QPushButton):

	def __init__(self, *args, **kwargs):
		super(Button, self).__init__(*args, **kwargs)
		self.__isShiftPressed = False

		self.clicked.connect(self.handleClick)

	def keyPressEvent(self, event):
		super(Button, self).keyPressEvent(event)
		self._processKeyEvent(event)

	def keyReleaseEvent(self, event):
		super(Button, self).keyReleaseEvent(event)
		self._processKeyEvent(event)

	def _processKeyEvent(self, event):
		isShift = event.modifiers() and QtCore.Qt.ShiftModifier
		self.__isShiftPressed = bool(isShift)

	def handleClick(self):
		if self.__isShiftPressed:
			print "Shift is pressed"
		else:
			print "Shift is not pressed"



# Window pointer.
window_ptr = OpenMayaUI.MQtUtil.findLayout("MASH")

try:
    instance = long(window_ptr)
except TypeError:
    pass  # Log error message

mash_widget =  shiboken.wrapInstance(instance, QtWidgets.QWidget)
mash_layout = mash_widget.layout()

random_button = Button("Something interesting")  # Create button.
mash_layout.addWidget(random_button)  # Add button
1 Like

Keep in mind if you do something like that and wrap the shelf button with a QPushbutton that it won’t be serialized to the shelf’s mel file. So you have to do that everytime you setup the shelf.

Natively the shelf buttons only support, click, double click, and some number of commands on a right click menu.

1 Like

Thanks, guys!
Just for a moment I was hoping the solution would be as simple as in Houdini…

if kwargs["ctrlclick"] == True: ...

That is what is so great with maya…
You can do pretty much anything if you know python, mel and c++. :wink:

Hi kiryha!

getModifiers is what you are looking for. :slight_smile:
Simple MEL example that you can use in your Shelf button:

int $mods = `getModifiers`
if ($mods % 2) { // Shift
   // do A
} else {
   // do B
}

Cheers,
Fabian

3 Likes

Oh, that looks better, thanks Fasbue!