0

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

  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字典以包含可以设置为的双击键Trueor False,但我不知道如何知道该键是否已连续按下两次。

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

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()
4

1 回答 1

2

一种可能的解决方案是保留一个属性last_pressed来跟踪最近按下的键。而不是使用计时器来调用keyAction,只需keyAction从按键按下和释放事件中调用。现在在keyPressEventif timerisActivelast_pressed == event.key()中,你知道这是一个“双”键按下。否则,分配最后按下的键并启动计时器。如果计时器超时,请重置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()
于 2020-03-21T23:29:41.730 回答