ceptno ceptno - 11 days ago 4x
Python Question

Built-in ways to detect keystrokes

I have been looking through so many questions exactly like this, but I have yet to find an answer. I have looked through this, but it is really complicated, and I don't know how to run my application while detecting keystrokes this way. Is there a built in python module for detecting keystrokes in a way that is threaded (sorry if that's the wrong term) so I don't have to keep checking in a while loop?


Yes, there is the curses module on UNIX. AFAIK there is no platform-independent module for doing what you want.

If you want something simple and that works everywhere it's probably better to use a GUI library such as Qt, Wx or Gtk. This does not mean that you have to write a full GUI for your program, but it isn't too hard to use the gui's event loop to catch keystrokes. For Qt you could use a QCoreApplication for example.

There are some ways to achieve what you want without external dependencies, for example this recipe which uses msvcrt on windows and tty, termios and sys on UNIX, but, since you complained about their complexity you're probably better with a gui toolkit.