<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#ffffff">
    Le 15/03/11 10:47, Samuele Carcagno a écrit :
    <blockquote cite="mid:201103151047.02310.sam.carcagno@gmail.com"
      type="cite">
      <pre wrap="">Hi list users,

I'm building a program to perform some auditory tests.
The user is presented with some sounds and then has to
press a button on the numeric keypad depending on which
sound was presented. I would like that button presses
given while the sounds are being presented were ignored,
and only button presses given after the sounds have finished
playing were accepted. I've been unable to achieve this,
two simplified examples are attached. Clicking on the button
starts a trial (in the actual program this starts the presentation
of the sounds, here it just waits for 3 seconds). Keyboard responses
given during the trial seem to be queued up and processed at the end
of the trial. Even if I set a flag, as in Example 2, 
to process the key presses only if self.trialRunning is False, this
doesn't seem to work, the keypresses seem to be queued up and processed
after the trial.

Thanks for any help.

Sam


##Example 1 ========================
import sys, time
from PyQt4 import QtGui
from PyQt4 import QtCore

class Example(QtGui.QWidget):
  
    def __init__(self):
        super(Example, self).__init__()
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Checkbox')

        self.button1 = QtGui.QPushButton('Button 1', self)
        QtCore.QObject.connect(self.button1,
                               QtCore.SIGNAL('clicked()'), self.onClickButton1)

    def onClickButton1(self):
        self.doTrial()

    def doTrial(self):
        print('Trial running') #this is where the sounds are presented
        time.sleep(3)

    def keyPressEvent(self, event):
        if (event.type() == QtCore.QEvent.KeyPress):
            if event.key()==QtCore.Qt.Key_0:
                buttonClicked = 0
            elif event.key()==QtCore.Qt.Key_1:
                buttonClicked = 1
            elif event.key()==QtCore.Qt.Key_2:
                buttonClicked = 2
            elif event.key()==QtCore.Qt.Key_3:
                buttonClicked = 3
            else:
                buttonClicked = 0
            print(buttonClicked)
            

if __name__ == '__main__':
  
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()
#*****************************************


#Example 2 ===============================
import sys, time
from PyQt4 import QtGui
from PyQt4 import QtCore

class Example(QtGui.QWidget):
  
    def __init__(self):
        super(Example, self).__init__()
        self.setGeometry(300, 300, 250, 150)
        self.trialRunning = False
        self.button1 = QtGui.QPushButton('Button 1', self)
        QtCore.QObject.connect(self.button1,
                               QtCore.SIGNAL('clicked()'), self.onClickButton1)

    def onClickButton1(self):
        self.doTrial()

    def doTrial(self):
        self.trialRunning = True
        print('Trial running') #this is where the sounds are presented
        time.sleep(3)
        self.trialRunning = False

    def keyPressEvent(self, event):
        if (event.type() == QtCore.QEvent.KeyPress) and self.trialRunning == False:
            if event.key()==QtCore.Qt.Key_0:
                buttonClicked = 0
            elif event.key()==QtCore.Qt.Key_1:
                buttonClicked = 1
            elif event.key()==QtCore.Qt.Key_2:
                buttonClicked = 2
            elif event.key()==QtCore.Qt.Key_3:
                buttonClicked = 3
            else:
                buttonClicked = 0
            print(buttonClicked)
            

if __name__ == '__main__':
  
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()

#*****************************************
_______________________________________________
PyQt mailing list    <a class="moz-txt-link-abbreviated" href="mailto:PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a>
<a class="moz-txt-link-freetext" href="http://www.riverbankcomputing.com/mailman/listinfo/pyqt">http://www.riverbankcomputing.com/mailman/listinfo/pyqt</a>

</pre>
    </blockquote>
    Logic, time() stop the process.<br>
    <br>
    Place your sound in a thread, like this:<br>
    <br>
    from threading import Timer<br>
    <br>
        def doTrial(self):<br>
            self.trialRunning = True<br>
            print('Trial running') #this is where the sounds are
    presented<br>
            self.t = Timer(3, self.unblock)<br>
            self.t.start()<br>
    <br>
        def unblock(self):<br>
            self.trialRunning = False<br>
    <br>
        def keyPressEvent(self, event):<br>
            print self.trialRunning<br>
            if self.trialRunning:<br>
                event.accept()<br>
            elif (event.type() == QtCore.QEvent.KeyPress) and
    self.trialRunning == False:<br>
                ...<br>
    <br>
    <div class="moz-signature">-- <br>
      Vincent V.V.<br>
      <a href="https://launchpad.net/oqapy">Oqapy</a></div>
  </body>
</html>