Skip to content

Instantly share code, notes, and snippets.

@justinfx
Created December 3, 2012 18:59
Show Gist options
  • Save justinfx/4197119 to your computer and use it in GitHub Desktop.
Save justinfx/4197119 to your computer and use it in GitHub Desktop.
A QPushButton that checks for the shift key modifier
"""
Record various key press states from a custom QPushButton
"""
from PyQt4 import QtGui, QtCore
class Button(QtGui.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() & QtCore.Qt.ShiftModifier
self.__isShiftPressed = bool(isShift)
def handleClick(self):
print "Shift pressed?", self.__isShiftPressed
if __name__ == "__main__":
app = QtGui.QApplication([])
button = Button("Click me")
button.show()
button.raise_()
app.exec_()
"""
Check the keyboard modifiers from the static QApplication call
"""
from PyQt4 import QtGui, QtCore
def handleClick():
mods = QtGui.QApplication.keyboardModifiers()
isShiftPressed = mods & QtCore.Qt.ShiftModifier
print "Shift pressed?", bool(isShiftPressed)
if __name__ == "__main__":
app = QtGui.QApplication([])
button = QtGui.QPushButton("Click me")
button.clicked.connect(handleClick)
button.show()
button.raise_()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment