Clearing the keyboard buffer (wxPython)

Mike Driscoll kyosohma at gmail.com
Wed Feb 11 15:45:56 EST 2009


On Feb 11, 2:28 pm, "MarcusD" <sto... at part-gmbh.de> wrote:
> Platform: MSW (XP) Python 2.5 wxPython 2.8
> Topic: Clear Keyboard buffer
>
> Hi,
> I hope I am not off topic asking a wxpython question.
> I'm writing a Score counter for Dart games. The software
> shows graphical output on a Canvas and accepts keyboard
> input (no buttons, no widgest). 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.NextPlayer()
> #--------------
>     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.HurryupandPlay(wx.SOUND_SYNC)
> #--------------
>
> The problem is, that during the voice output all keystrokes
> are buffered somewhere and are processed when SayScore is
> finished. So I have to clear the keyboard buffer at the end
> of SayScore. Something like:
>
>   evt=MagicFetchPendingKeyEventFunction()
>   while evt:
>     evt=MagicFetchPendingKeyEventFunction()
>
> I was thinking about threading (maybe using delayedresults), but
> this might be a little oversized for my problem.
> Playing the sound ASYNC doesn't work here.
>
> Any ideas?
> Thanks in advance
> Marcus
It's fine to post here with wxPython questions, but I would recommend
the official wxPython mailing list in most cases as it has lots of
experienced users and developers on it: http://wxpython.org/maillist.php

I've never done this sort of thing with wxPython, but you may be able
to use wx.Yield somehow. There are various tips and tricks in the wiki
about working around long running processes that block the GUI's
mainloop here:

http://wiki.wxpython.org/LongRunningTasks

Other than that, I would recommend doing this with pygame or pyglet as
they might be easier to use for this use-case. Here are their sites:

http://www.pyglet.org/
http://www.pygame.org/news.html

HTH

Mike



More information about the Python-list mailing list