Skip to content
Advertisement

Python Get File Length Of Keyboard Buffer

I am coding a text-based game, and I need to read key presses. For various reasons, I’m doing this by reading the keyboard buffer. This is my current code for that:

JavaScript

The problem is, that the program will wait until it has read 45 bytes. I do not want this, because the program should do other stuff (like recharging mana). So I thought the program could check to see if the file is empty, and if it isn’t, there would have been a new key press and I could read it. So I wrote this code:

JavaScript

But os.stat("dev/input/event3").st_size returned 0. I also tried with os.path.getsize("/dev/input/event3"), but it also returned 0.

I have considered threading as a possible solution, but it is a bit too complicated for me, especially with transferring variables from one thread to another (and Lock() and similar things are beyond my understanding.

So my question is: Why does this happen, and how can I fix it?

Advertisement

Answer

I have found a solution, that uses threads, but not that complicated. Add this function:

JavaScript

To look if a key is being pressed/was pressed:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement