Welcome, guest | Sign In | My Account | Store | Cart
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PyQt4.QtCore import Qt
from PyQt4.QtGui import QApplication
import functools

modies = { 'shift': Qt.ShiftModifier,
           'control': Qt.ControlModifier,
           'alt': Qt.AltModifier,
           'meta': Qt.MetaModifier }

def check_modifiers(org_meth):
    """Add modifiers kwarg to a method that contains a tuple of currently pressed modifiers."""

    @functools.wraps(org_meth)
    def wrapper(*args, **kwargs):
        curr = QApplication.keyboardModifiers()
        kwargs['modifiers'] = tuple( name for name, which in modies.items() if curr & which == which )

        org_meth(*args, **kwargs)

    return wrapper


if __name__ == '__main__':

    import sip
    from PyQt4 import QtGui, QtCore

    class MainWindow(QtGui.QMainWindow):

        def __init__(self):
            super(MainWindow, self).__init__()


            centralWidget = QtGui.QWidget(self)
            layout = QtGui.QHBoxLayout(centralWidget)
            self.setCentralWidget(centralWidget)
            self.clickButton = QtGui.QPushButton("click", centralWidget)
            self.clickButton.clicked.connect(self.klick)
            layout.addWidget(self.clickButton)
            self.statusBar()
            self.setFixedWidth(600)

        @check_modifiers
        def klick(self, event, modifiers):
            ms = QtGui.QApplication.keyboardModifiers()
            m = "keyboardModifiers: {1:0=32b} {0} has been pressed"
            self.statusBar().showMessage(m.format(repr(modifiers), int(ms)))
            
    import sys
    app = QtGui.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())

Diff to Previous Revision

--- revision 1 2013-09-25 15:56:18
+++ revision 2 2013-09-29 14:13:04
@@ -1,8 +1,9 @@
-#!/usr/bin/env python3
+#!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
 from PyQt4.QtCore import Qt
 from PyQt4.QtGui import QApplication
+import functools
 
 modies = { 'shift': Qt.ShiftModifier,
            'control': Qt.ControlModifier,
@@ -10,21 +11,21 @@
            'meta': Qt.MetaModifier }
 
 def check_modifiers(org_meth):
-    """Add modifiers kwarg to a method that contains a tuple of names of currently pressed modifiers."""
+    """Add modifiers kwarg to a method that contains a tuple of currently pressed modifiers."""
 
-    def check_modifiers(*args, **kwargs):
+    @functools.wraps(org_meth)
+    def wrapper(*args, **kwargs):
         curr = QApplication.keyboardModifiers()
         kwargs['modifiers'] = tuple( name for name, which in modies.items() if curr & which == which )
 
-        org_meth.__call__(*args, **kwargs)
+        org_meth(*args, **kwargs)
 
-    return check_modifiers
+    return wrapper
 
 
 if __name__ == '__main__':
 
     import sip
-    sip.setapi('QVariant', 2)
     from PyQt4 import QtGui, QtCore
 
     class MainWindow(QtGui.QMainWindow):
@@ -40,10 +41,13 @@
             self.clickButton.clicked.connect(self.klick)
             layout.addWidget(self.clickButton)
             self.statusBar()
+            self.setFixedWidth(600)
 
         @check_modifiers
         def klick(self, event, modifiers):
-            self.statusBar().showMessage( repr(modifiers) + " have been pressed")
+            ms = QtGui.QApplication.keyboardModifiers()
+            m = "keyboardModifiers: {1:0=32b} {0} has been pressed"
+            self.statusBar().showMessage(m.format(repr(modifiers), int(ms)))
             
     import sys
     app = QtGui.QApplication(sys.argv)

History