platform independent kbhit()

Scott David Daniels Scott.Daniels at Acm.Org
Mon Jan 17 12:15:34 EST 2005

Hans Georg Krauthaeuser wrote:
> I use msvcrt.kbhit() to check for a user keyboard event on windows. But 
> now, I would prefer to make the module independent from the platform 
> used. 
This is not in general possible; many machines do not have keyboards.
You can, perhaps, build one for yourself from a pair of implementations
or more.  Portable software seldom comes from adding one platform at a
time that the software works on.  Portable software comes from working
using only features common (or in rare circumstances available) on all
machines in your target set, and then adding enough tests to believe
the portability.  CPython starts with the C89-supported environment as
its base.  It tries hard to stick to that abstract machine.  I assume
Jython does a similar thing using Java VM semantics that it trusts will
be common across implementations.

 > I already know that I can use curses (on linux/unix) or Tkinter.
> Also, I  found this C 
> source that has a kbhit() and a getch() for linux/unix that I can SWIG 
> to python.
Either of these might be a good basis for your personal "all machines I
care about" semantics.  If you use curses, make sure it doesn't impose
an extra constraint on all terminal access.

--Scott David Daniels
Scott.Daniels at Acm.Org

More information about the Python-list mailing list