magmax/python-readchar

feature request - non-blocking / timeout

Opened this issue · 4 comments

2sn commented

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?

2sn commented

Excellent. Thanks a lot!

we will see when magmax comes back and merges it 😃

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?