Skip to content

feature request - non-blocking / timeout #62

Open

Description

@2sn

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?

Activity

  1. 2sn commented on Jul 5, 2022

    @2sn
    Author

    Excellent. Thanks a lot!

  2. Cube707 commented on Jul 5, 2022

    @Cube707
    Collaborator

    we will see when magmax comes back and merges it 😃

  3. Cube707 commented on Jul 20, 2022

    @Cube707
    Collaborator

    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?

  4. linked a pull request that will close this issueContexmanager #96on Oct 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions