Clearing the keyboard buffer or kill pending events

Platform: MSW (XP) Python 2.5 wxPython 2.8
Topic: Clear Keyboard buffer

Hi,
my application shows graphical output on a Canvas and
accepts keyboard input (no buttons, no widgets, no mouse).
When enter is pressed, some calculations are done and
the result is presented as voice output (concatenating
and playing some wav files).

The problem is, that while playing the sounds the user
could press further buttons. Their events are buffered
somewhere and are processed when SayScore is finished.
I don't want that. So I have to:

1. Disable OnKeyEvent before calling SayScore, or
2. clear the keyboard buffer after SayScore

Yield doesn't work because I don't want to process any
keys during voice output.
Threads don't work because then several voice outputs
could appear at the same time.
Playing the sounds ASYNC doesn't work for the same
reason.

I did find the function app.Pending() and I look for
something like:

while app.Pending():
  if KeyEvent:
    app.KillThatKeyEvent()
  
at the end of SayScore.

A stripped version looks
like that:

self.Bind(wx.EVT_CHAR,OnKeyEvent)
.
.

···

#--------------
    def OnKeyEvent(self, event):
      k =event.GetKeyCode()
      try:
        z=chr(k)
      except:
        return
      if (z in ["0","1","2","3","4","5","6","7","8","9",.."\r"..]:
        self.keystr +=z
        score=self.CalculateScore(self.keystr)
        self.DisplayScore(score)
        .
        .
      if z== "\r" #enter
        self.SayScore(score)
        self.NextInput()
#--------------
    def SayScore(self,number):
      ...
      if number > 100:
        a=os.path.join(self.wavpath,"100.wav")
        b=os.path.join(self.wavpath,"%i.wav"%(number-100))
      else:
        a=os.path.join(self.wavpath,"%i.wav"%(number))
        b=""
      sound1 = wx.Sound(a)
      sound2 = wx.Sound(b)
      ...
      sound1.Play(wx.SOUND_SYNC)
      if b:
        sound2.Play(wx.SOUND_SYNC)
      ...
#--------------

Any ideas?
Thanks in advance
Marcus

--
View this message in context: http://www.nabble.com/Clearing-the-keyboard-buffer-or-kill-pending-events-tp21973194p21973194.html
Sent from the wxPython-users mailing list archive at Nabble.com.

MarcusD wrote:

Platform: MSW (XP) Python 2.5 wxPython 2.8
Topic: Clear Keyboard buffer

Hi,
my application shows graphical output on a Canvas and accepts keyboard input (no buttons, no widgets, no mouse). When enter is pressed, some calculations are done and
the result is presented as voice output (concatenating
and playing some wav files).

The problem is, that while playing the sounds the user
could press further buttons. Their events are buffered
somewhere and are processed when SayScore is finished.
I don't want that. So I have to:

1. Disable OnKeyEvent before calling SayScore, or

This is easy to do. Either set a flag that you check at the beginning of OnKeyEvent and return immediately if it is set, or you can unbind the event handler with Unbind.

2. clear the keyboard buffer after SayScore

This is not doable from wx, it would take platform specific code to do.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

1. Disable OnKeyEvent before calling SayScore, or

This is easy to do. Either set a flag that you check at the beginning
of OnKeyEvent and return immediately if it is set, or you can unbind the
event handler with Unbind.

I tried that. Setting a flag before SayScore and resetting it at the end
of SayScore. This doesn't work because OnKeyEvent isn't called while
the sounds are played. So I set the flag at the beginning of SayScore
and added in OnKeyEvent:

if flag:
   if app.Pending()
     return
   else:
     flag=0

which works fine. Is it a problem that i don't know what kind
of event is pending?

I will check Unbind, which is not listed in my wxWidgets docu.

Thanks.

2. clear the keyboard buffer after SayScore

This is not doable from wx, it would take platform specific code to do.

Good to know. So I can stop searching. Strange that nobody else seems
to need this. Can't be poor design, can it?

Marcus

···

--
View this message in context: http://www.nabble.com/Clearing-the-keyboard-buffer-or-kill-pending-events-tp21973194p22042355.html
Sent from the wxPython-users mailing list archive at Nabble.com.