user1610406
user1610406

Reputation: 782

How do I get realtime keyboard input in Python?

Is this possible? Every answer I have looked at isn't what I want. What I do though is something like in omega-rpg (which is an awesome little text-based debian rpg), but in Python instead of C. I have poked around and found some things, but nothing that's relevant to what I'm doing. Is it just easier to use raw_input() / input, or would it be more efficient to use some kind of API for doing so?

TO CLEAR UP:

I need a system of realtime keyboard input in Python, but I don't know whether it's easier to use an API or just raw_input() / input(). If it IS better to use an API, which is the best one for a beginner-level programmer to use?

EXTRA:

Any solution will be used in a TEXT-BASED GAME! Keep that in mind when answering, please.

Upvotes: 4

Views: 9090

Answers (3)

Manoj I
Manoj I

Reputation: 1099

You could look into PyHook. It provides callbacks for reading mouse and keyboard events.

Upvotes: 0

Tim
Tim

Reputation: 12174

If you are using Windows you can use the msvcrt module. Specifically, look at kbhit to check whether a key is waiting to be read and getch to read a key press. The user does not need to press enter for the key presses to be made available to your program.

Upvotes: 0

timc
timc

Reputation: 2174

I think using raw_input() in a while loop is an acceptable solution.

You could also look into something like pygame which would handle the main game loop for you and also offers input handling.

Upvotes: 5

Related Questions