I am using python 3 and whenever I execute a python script like:
import time
while (True):
print("-test-")
time.sleep(1)
on my linux terminal. Every keystroke (like "asdf" in line three) gets printed to the terminal as long as the terminal is focused:
-test-
-test-
asdf-test-
-test-
Is there a way to stop the terminal from outputting keystrokes while my python script is running (preferably without changing the configurations of the os or the terminal itself)?
More specifically I would like to use "pynput" to read keyboard events without the key appearing in my outputs:
from pynput import keyboard
import time
def key_pressed(key):
print("key {0} pressed".format(key))
if __name__ == "__main__":
listener = keyboard.Listener(on_press=key_pressed)
listener.start()
while(True):
print("-test-")
time.sleep(1)
which would output:
-test-
-test-
akey 'a' pressed
-test-
when key "a" was pressed after the second print("-test-")
instead of:
-test-
-test-
key 'a' pressed
-test-
I have found this:
How to prevent shell from getting input (keyboard) while running a python script?
which links to:
How do I 'lock the keyboard' to prevent any more keypresses being sent on X11/Linux/Gnome?
which as far as I could determine is not quite what I am looking for since they try to block keyboard input altogether. I only want to block it for the terminal the script is running in.