QKeyPress事件在PyQt [英] QKeyPress event in PyQt

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

问题描述

我有我的软件问题。在我的钢琴软件中,如果我继续按下键盘上的一个键,那么它会重复同一个特定键的相同音调。但实际上我需要一个单一的音调,直到释放那个特定的键。我提供了一部分代码来检测keyPress事件并调用相应的方法。那么我应该用我的代码来做什么变化?

  def keyPressEvent(self,event):
如果类型(事件)== QtGui.QKeyEvent和event.key()== QtCore.Qt.Key_A:
self.Playnote('/ home / hemanth / hemanth / Piano / C.mp3')
self.ui.pushButton.animateClick(100)
如果类型(事件)== QtGui.QKeyEvent和event.key()== QtCore.Qt.Key_S:
self.Playnote( '/home/hemanth/hemanth/Piano/D.mp3')
self.ui.pushButton_2.animateClick(100)


解决方案

您应该检查 event.isAutoRepeat() 以确定事件是由键盘自动重复或实际按键引起的。如果它是一个自动重复,忽略该按。


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)

解决方案

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.

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

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