PyQt 中的 QKeyPress 事件

2022-01-15 00:00:00 python pyqt qt event-handling piano

问题描述

我的软件有问题.在我的钢琴软件中,如果我一直按键盘上的一个键,那么它会为该特定键发出多个重复的相同音调.但实际上我需要一个单一的音调,直到释放那个特定的键.我提供了检测 keyPress 事件并调用相应方法的代码的一部分.那么我应该对我的代码进行哪些更改?

I am having issues with my software. In my piano software if I keep pressing a key on the keyboard then it is making multiple repeated same tones of that particular key. But actually I need a single tone until the release of that particular key. I'm providing a part of my code that detects the keyPress event and calls the corresponding method. So what should be the changes that I've to do with my code??

def keyPressEvent(self, event):
    if type(event) == QtGui.QKeyEvent and event.key() == QtCore.Qt.Key_A : 
        self.Playnote('/home/hemanth/hemanth/Piano/C.mp3')
        self.ui.pushButton.animateClick(100)
    if type(event) == QtGui.QKeyEvent and event.key() == QtCore.Qt.Key_S:
        self.Playnote('/home/hemanth/hemanth/Piano/D.mp3')
        self.ui.pushButton_2.animateClick(100)


解决方案

你应该检查 event.isAutoRepeat() 确定事件是由键盘自动重复还是实际按键引起的.如果是自动重复,请忽略该按.

You should check event.isAutoRepeat() to determine if the event is caused by keyboard autorepeat or an actual keypress. If it is an autorepeat, ignore that press.

相关文章