2016-02-13 68 views
0

問題:PyQt的和PySide:筆記本電腦和OS X上運行時,它是在兩個一致的代碼不會對Qt.AltModifier打印。有沒有辦法檢查QKeyEvent == ControlKey?Qt.AltModifier和OS X

from PyQt4 import QtCore, QtGui 


class Custom(QtGui.QWidget): 
    def __init__(self, *args, **kwargs): 
     QtGui.QWidget.__init__(self, *args, **kwargs) 

    def keyPressEvent(self, event): 

     if event.key()==QtCore.Qt.Key_A: 
      print 'QtCore.Qt.Key_A' 


     if event.key()==QtCore.Qt.ALT: 
      print 'QtCore.Qt.ALT' 

     if event.key()==QtCore.Qt.AltModifier: 
      print 'AltModifier' 


     if event.key()==QtCore.Qt.CTRL+QtCore.Qt.Key_A: 
      print 'QtCore.Qt.CTRL+QtCore.Qt.Key_B' 

if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    w = Custom() 
    w.show() 
    sys.exit(app.exec_()) 

回答

1

要檢查​​按鈕被按下,你需要檢查modifiers()改性劑,即:

if event.modifiers() == QtCore.Qt.AltModifier: 
    if event.key() == QtCore.Qt.Key_A: 
     print 'ALT+A' 
+0

完美!謝謝! – alphanumeric