You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
withKeyReader() asr:
spinner=cycle(["-", "/", "|", "\\"])
whileTrue:
print("\rpress a key: "+next(spinner), end="")
ifr.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?
Activity
Cube707 commentedon Jul 5, 2022
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 commentedon Jul 5, 2022
Excellent. Thanks a lot!
Cube707 commentedon Jul 5, 2022
we will see when magmax comes back and merges it 😃
Cube707 commentedon Jul 20, 2022
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 whenreadchar()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 ofreadcharand 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
readcharblock 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:
would this be of use to you if it was part of the libary?