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>I urge you to reconsider not using tkinter.  Ignoring the GUI stuff, it has a tested, cross-platform, <em>asynchonous</em> event loop system that handles both scheduled (timed) events and key press and release events.  And it is written in C and updated as platforms change.  (See the comments for the recipe @Ty quoted about platform changes.)  You will find it very difficult to reproduce the same facilities in Python.</p>

<p>Usage would start with</p>

<pre><code>import tkinter
root = tkinter.Tk()
root.withdraw()  # make the window invisible
</code></pre>

<p>Thereafter, one can ignore the GUI stuff and only use root.bind(key event, function) and root.after(milleseconds, function, *args).  (In the meanwhile, the option to use graphics to interact with the robot, in the future, would still be there.)</p>

<p>Given existing move_forward and stop_move functions, here is how to make a robot move while pressing up-arrow.</p>

<pre><code>root.bind('&lt;KeyPress-Up&gt;', move_forward)
root.bind('&lt;keyRelease-Up&gt;' stop_move)
</code></pre>

<p>About the importance of being asynchronous (non-blocking): I presume you call an update function 20 times a second by calling time.sleep(.05) between each update.  This is a blocking call and during this period your code is frozen.  It cannot do anything and cannot respond to events.</p>

<p>Suppose you develop two or more animations for the robot: say one for the arms, one for the head (or whatever is appropriate for your robot).  Each would consist of a series times commands, implemented by root.after calls.  Now suppose you want to run two or more animations in parallel.  Assuming that each only requires a small fraction of CPU time, this would be trivial with tkinter because each would run asynchronously and not block the program from doing other things.  With a blocking event system, you would have to write a new animation each each combination you want.</p>

python events keypress