1

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.

Aio
  • 101
  • 5

1 Answers1

0

Your keystrokes don't get printed. You write into the terminal. Just unfocus the terminal window and it will work perfectly fine. pynput also works if the window is in the background.

Edit: You could replace print("key {0} pressed".format(key)) with print("\bkey {0} pressed".format(key)). \b prints a backspace to the terminal. It deletes the key you just wrote into the terminal.

  • Thank you for your reply but I could not find a way to keep the the terminal out of focus while running the script (other than focusing something else manually). So for anything other than a small script I am writing for myself this won't be a solution I am afraid. – Aio Apr 22 '21 at 13:13
  • What's the problem with focusing another window? – Michael Hofmann Apr 22 '21 at 13:17
  • When running the terminal in "windowed-fullscreen" I have no other window to switch to. – Aio Apr 22 '21 at 13:31
  • That makes sense. Have a look at my edit though – Michael Hofmann Apr 22 '21 at 13:36
  • Again thank you but the `print("key {0} pressed".format(key))` was just an example to clarify the problem my real output is a matrix of ASCII-symbols that fills the entire terminal window. Since all this is for a module I want to use for other projects (which I might share with other people) I was looking for a more general solution. I considered deleting (or overwriting) the keys but that would be a bit more difficult when the cursor was sitting anywhere else than the end of the window and the input would still blink onto the screen after each keystroke. – Aio Apr 22 '21 at 13:44