event + button click

43 views
Skip to first unread message

Rudi Hammad

unread,
Jan 13, 2022, 8:29:35 AM1/13/22
to Python Programming for Autodesk Maya
Hello

I am trying to have a button that print "a" if the button is clicked while holding shift, and "b" while holding control.
This is what I have done (since it is not too long I've attached it here)

from PySide2 import QtWidgets, QtCore, QtGui

class XX(QtWidgets.QPushButton):

    def __init__(self):
        super(XX, self).__init__()
        self.setText("myCustomBtn")

    def keyPressEvent(self, event):
        if event.type() == QtCore.QEvent.KeyPress and self.isDown():
            if event.key() == QtCore.Qt.Key_Shift:
                print "a"
            if event.key() == QtCore.Qt.Key_Control and self.isDown():
                print "b"
       
        return QtCore.QObject.event(self, event)

x = XX()
x.show()

This kind of works but only if you click and keep the button down and the press a key y does what I want. Obviosly this is not what I want.

Any suggestions?
thanks

R




Marcus Ottosson

unread,
Jan 13, 2022, 9:02:23 AM1/13/22
to python_in...@googlegroups.com

Try getting the modifier state from here.

modifiers = QtWidgets.QApplication.keyboardModifiers()

if modifiers & QtCore.Qt.ControlModifier:
  print("Ctrl was held")

if modifiers & QtCore.Qt.ShiftModifier:
print("Shift was held")

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/4bebba97-bd1b-427f-93db-a0f1afba0eedn%40googlegroups.com.

Rudi Hammad

unread,
Jan 13, 2022, 10:07:20 AM1/13/22
to Python Programming for Autodesk Maya
thanks marcus, that did it.

Rudi Hammad

unread,
Jan 13, 2022, 10:12:59 AM1/13/22
to Python Programming for Autodesk Maya
aahh,  of course more platform related fun:
>> Note: On macOS, the ControlModifier value corresponds to the Command keys on the keyboard

At the end I guess I will have to install a mac virtual machine.
Reply all
Reply to author
Forward
0 new messages