magmax / python-readchar

Python library to read characters and key strokes
MIT License
143 stars 45 forks source link

feature request - non-blocking / timeout #62

Open 2sn opened 3 years ago

2sn commented 3 years ago

Is there a way to have a non-blocking version that returns None or raises an exception if there no current key?

Or, even better, to have a timeout, and then do the above after the timeout has passed and no key was pressed or no character entered, respectively?

Cube707 commented 2 years ago

I implemented the non-blocking version in #79

See here: https://github.com/Cube707/python-readchar/blob/dev-v4/README.rst#readcharblockingtrue---str--none

https://github.com/Cube707/python-readchar/blob/c3081ec51581ead9b4db2f60a8f1567ed809d06a/readchar/posix_read.py#L17-L22

https://github.com/Cube707/python-readchar/blob/c3081ec51581ead9b4db2f60a8f1567ed809d06a/readchar/win_read.py#L8-L13

2sn commented 2 years ago

Excellent. Thanks a lot!

Cube707 commented 2 years ago

we will see when magmax comes back and merges it 😃

Cube707 commented 2 years ago

So, turns out stuff is more complex on the linux side... Which is why I removed this feature form the v4 development for now.

When working on linux systems, we need to reconfigure the terminal to fit readchars needs (don't echo pressed keys back to the user, don't stop on CTRL+C, etc.). This happens when readchar() is called and gets reset before the function finishes. This is fine when blocking untill a key is pressed but when spending a lot of time outside of readchar and little time inside it to actually check for keypresses, this leads to unexpected behavior in form of random echoed letters.

The easiest soloution is probably to use multiple threads and have readchar block one of them and wait for input, this way the thread could also be terminated if need be. (See here for example.)

To get this behavior working on a single thread the user would have to first set up the Terminal, then run his code and then runs some more code to reset the terminal. This would call for a context-manager and is defnetly possible, but making it libary read is difficult and it would add much more complexety when using the libary. Here is a example of how it could look:

with KeyReader() as r:
    spinner = cycle(["-", "/", "|", "\\"])
    while True:
        print("\rpress a key: " + next(spinner), end="")
        if r.kbhit():
            k = r.readkey()
            print(f"\r{k.encode()}-key was pressed")

        # do other stuff

would this be of use to you if it was part of the libary?