PyQt 中的 QKeyPress 事件 [英] QKeyPress event in PyQt

查看:32
本文介绍了PyQt 中的 QKeyPress 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的软件有问题.在我的钢琴软件中,如果我一直按键盘上的一个键,那么它会为该特定键发出多个重复的相同音调.但实际上我需要一个单一的音调,直到释放那个特定的键.我提供了检测 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.

这篇关于PyQt 中的 QKeyPress 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆