Python: how would I write a keyevent?
<p>So simply put I want my code to call a event like OnKeyPress("Keyname") or something, each time I press a key. I don't want to use tkinter.</p>

<p>I have a Update method that gets called 20 times a second that's in a parent class
I have several child classes that have that function called. On some of them I want to use a key press event to make parts of my robot I am working on move.(eg: while I am pressing the up arrow the robot will move foreword) tkinter is well not designed for this its more for drawing content on a screen and I want to make my code more efficient for its purpous. also I would prefer to have my own then use a pre-made one so I can get a better understanding on how it works.</p>

<p>This should work
<pre>
class _GetchUnix:
    def <strong>init</strong>(self):
        import tty, sys
    def <strong>call</strong>(self):
        import sys, tty, termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(sys.stdin.fileno())
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch
class _GetchWindows:
    def <strong>init</strong>(self):
        import msvcrt
    def <strong>call</strong>(self):
        import msvcrt
        return msvcrt.getch()
class _Getch:
    def <strong>init</strong>(self):
        try:
            self.impl = _GetchWindows()
        except ImportError:
            self.impl = _GetchUnix()</p>

<code>    def __call__(self): return self.impl()
getch = _Getch()
while True:
    if getch.impl():
        print getch.impl()
</code></pre>

<p>Every thing up to the <code>while</code> loop you need. Then when ever you call <code>getch.impl()</code> it will wait for a input, but if there is none, it will move on. That's why I used the <code>while</code> loop</p>

<p><strong>DISCLAIMER</strong><br>
This is not my code, please check out:<br>
<a href="http://code.activestate.com/recipes/134892/" rel="nofollow">http://code.activestate.com/recipes/134892/</a></p>

python events keypress