PyQt5 - 双击相同的键盘键

PyQt5 - Double press same keyboard key

我通过子类化 QMainWindow 并覆盖 keyPressEventkeyReleaseEvent 函数来处理多个按键,然后在按键是否按下时更新字典...

  def keyPressEvent(self,event):
    self.pressedKeys[event.key()] = True

  def keyReleaseEvent(self,event):
    self.pressedKeys[event.key()] = False

... 而 pressedKeysArray 是:

  pressedKeys = {Qt.Key_Left: False,
                 Qt.Key_Right: False,
                 Qt.Key_Down: False,
                 Qt.Key_Up: False}

但是,我希望能够捕捉到双右箭头单击,比如说在 100 或 200 毫秒之内。我会扩展 pressedKeys 字典以包含可以设置为 TrueFalse 的双击键,但我不知道如何快速知道该键是否被按下两次继承.

到目前为止,这是我的代码:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys

class MainWindow(QMainWindow):
  pressedKeys = {Qt.Key_Left: False,
                 Qt.Key_Right: False,
                 Qt.Key_Down: False,
                 Qt.Key_Up: False}

  def __init__(self,parent=None,*args,**kwargs):
    QMainWindow.__init__(self,parent,*args,**kwargs)
    self.parent = parent

    self.timer = QTimer()
    self.timer.timeout.connect(self.keyAction)
    self.timer.start(50)

    self.show()

  def keyPressEvent(self,event):
    self.pressedKeys[event.key()] = True

  def keyReleaseEvent(self,event):
    self.pressedKeys[event.key()] = False

  def keyAction(self):
    if self.pressedKeys[Qt.Key_Left] and self.pressedKeys[Qt.Key_Down]: print("rolling left")

    elif self.pressedKeys[Qt.Key_Right] and self.pressedKeys[Qt.Key_Down]: print("rolling right")

    elif self.pressedKeys[Qt.Key_Left]: print("running left")

    elif self.pressedKeys[Qt.Key_Right]: print("running right")

    elif self.pressedKeys[Qt.Key_Down]: print("crouching") #return here to block effect of 'up' key being pressed

    if self.pressedKeys[Qt.Key_Up]: print("jumping")

def exceptHook(exectype,value,traceback):
  sys.__excepthook__(exectype,value,traceback)

if __name__ == "__main__":
  sys.excepthook = exceptHook
  app = QApplication(sys.argv)
  mainWindow = MainWindow()
  app.quit()

一个可能的解决方案是保留一个 属性 last_pressed 来跟踪最近按下的键。不用定时器调用 keyAction,只需从按键和释放事件中调用 keyAction。现在在 keyPressEvent 如果计时器 isActivelast_pressed == event.key(),你知道这是一个 "double" 按键。否则,分配最后按下的键并启动计时器。如果计时器达到超时,则重置 last_pressed

class MainWindow(QMainWindow):

    pressedKeys = {Qt.Key_Left: False, Qt.Key_Right: False,
                   Qt.Key_Down: False, Qt.Key_Up: False}

    def __init__(self,parent=None,*args,**kwargs):
        QMainWindow.__init__(self,parent,*args,**kwargs)
        self.parent = parent
        self.last_pressed = None

        self.timer = QTimer()
        self.timer.setSingleShot(True)
        self.timer.timeout.connect(self.clear_pressed)

        self.show()

    def clear_pressed(self):
        self.last_pressed = None

    def keyPressEvent(self,event):
        self.pressedKeys[event.key()] = True
        if self.timer.isActive():
            if self.last_pressed == event.key():
                print('Double')
                self.timer.stop()
        else:
            self.timer.start(200)
        self.last_pressed = event.key()
        self.keyAction()

    def keyReleaseEvent(self,event):
        self.pressedKeys[event.key()] = False
        self.keyAction()