Only Key_Tab & ShiftModifier does't work well

2019-08-24 13:57发布

问题:

Pre

I searched other Questions and couldn't find out the solution.


I want to execute Tab key and Shift operation simultaneously because I want to add a new performance by pressing tab key.I know the Shiftmodifier enum is good. But it doesn't work when the key is Tab key.Do you know how to control it? On the other hand,Controlmodifier works well.

When I pushed Tab key

tab only

When I pushed Tab & Control Key

tab & Control

When I pushed Any Key except for Tab & Shift Key

print("tab & any key except for tab key")

When I pushed Tab & Shift Key

No Response... Why?

Sample Code

from PySide import QtGui
from PySide import QtCore
import sys

class TSEditer(QtGui.QTextEdit):
    def __init__(self,parent=None):
        super(TSEditer,self).__init__(parent=None)

    def keyPressEvent(self,event):


        if event.key() == QtCore.Qt.Key_Tab and event.modifiers() == QtCore.Qt.ControlModifier:
            print("tab & control")
        elif event.key() == QtCore.Qt.Key_Tab and event.modifiers() == QtCore.Qt.ShiftModifier:
            print("tab & shift")
        elif event.key() == QtCore.Qt.Key_A and event.modifiers() == QtCore.Qt.ShiftModifier :
            print("tab & any key except for tab key")
        elif event.key() == QtCore.Qt.Key_Tab:
            print("tab only")

        return QtGui.QTextEdit.keyPressEvent(self,event)

def main():
    try:
        QtGui.QApplication([])
    except Exception as e:
        print(15,e)
    ts = TSEditer()
    ts.show()
    sys.exit(QtGui.QApplication.exec_())
if __name__ == "__main__":
    main()

回答1:

You should search the event.key() value.

def keyPressEvent(self,event):
    print(event.key())

and the value is 16777218 when I pushed shift key and tab key.

So you can do it by writing your code like this.

From:

def keyPressEvent(self,event):
    if event.key() == QtCore.Qt.Key_Tab and event.modifiers() == QtCore.Qt.ShiftModifier:
       #Code

To:

def keyPressEvent(self,event):
    if event.key() == 16777218:
       #Code

Probably,there is a bug. 16777218 means key_shift and key_tab are pressed. you can do the same thing.



标签: pyside