Search code examples
pythonkeyboardvpython

How to deactivate keyboard buffer in Windows?


I'm trying to program something using vPython. It's kind of a game, but controls won't work properly.

while True:
    "verarbeitet Maus/Tastatureingaben"        
    if scene.kb:                        # wenn Aktion auf der Tastatur...
        druck=scene.kb.getkey()         # ...Tastendruck speichern!
        # ----Aktionen bei bestimmten Tasten---- #
        if druck == "w":            # vor
            self.bewegen(self.axis)
        elif druck == "s":          # zurück
            self.bewegen(-self.axis)

So there are two main problems:

  1. Two keys can't be pressed at the same time. Only the one pressed latest is working.

  2. If holding a key for about 5 seconds the action will take much longer (I think that's because MS Windows takes a small break after each 'hit').

I hope you can help me!


Solution

  • did you try running your script with -u option:

    python -u myscript.py
    

    You can find documentation on this option here

    As per document:

    Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, 
    also put stdin, stdout and stderr in binary mode.