简体   繁体   English

捕获PYQT4中的密钥

[英]Capture Keys in PYQT4

I am trying to capture all of key pressed in my program 我正在尝试捕获程序中所有按下的键

def keyPressEvent(self, event):

    event = event.key()

    if (event == QtCore.Qt.Key_Q):
        print ('Q!.')

That function works fine when i am trying to capture keys in my window. 当我尝试捕获窗口中的键时,该功能可以正常工作。 (in this case Q_Key) (在这种情况下为Q_Key)

But if the key was pressed in a Text Widget (for example in a: QListView, QlistWidget, QLineEdit, and so many more ) it don't work. 但是,如果在Text Widget中按下了键(例如,在QListView,QlistWidget,QLineEdit等中),则此键将无效。 The function prints nothing. 该功能不打印任何内容。 I am doing something wrong... What can i do to fix it? 我做错了什么...我该怎么解决?

Thanks and sorry for my english! 谢谢,抱歉我的英语!

You will need to install an event-filter on the application to get all key-press events: 您将需要在应用程序上安装事件过滤器,以获取所有按键事件:

class Window(QtGui.QWidget):
    def __init__(self):
        ...
        QtGui.qApp.installEventFilter(self)

    def eventFilter(self, source, event):
        if event.type() == QtCore.QEvent.KeyPress:
            print('KeyPress: %s [%r]' % (event.key(), source))
        return super(Window, self).eventFilter(source, event)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM