PyQt: keyPressEvent and keyReleaseEvent - holding

2019-02-21 02:35发布

I want implement a QDialog, in which a key is pressed for some time (seconds) and is released afterwards.

I tried doing this with keyPressEvent and keyReleaseEvent functions.

The problem I get is that when a key is pressed the debounce (keyReleaseEvent) occurs very quickly, which is not desired in my case.

Here, is what I do:

def keyPressEvent(self, eventQKeyEvent):
    key = eventQKeyEvent.key()
    if key == 54:
    print('pressed')  # 6


def keyReleaseEvent(self, eventQKeyEvent):
    key = eventQKeyEvent.key()
    if key == 54:
    print('released')  # 6

Output:

'pressed'
'released'
'pressed'
'released'
'pressed'
'released'
...
'pressed'
'released'

I would like to get either:

'pressed'
'released'

or:

'pressed'
'pressed'
'pressed'
...
'pressed'
'pressed'
'released'

标签: qt pyqt pyside
1条回答
The star\"
2楼-- · 2019-02-21 02:56

You need to use isAutoRepeat:

def keyReleaseEvent(self, eventQKeyEvent):
    key = eventQKeyEvent.key()
    if key == 54 and not eventQKeyEvent.isAutoRepeat():
        print('released')
查看更多
登录 后发表回答