How to hide keyboard echo to terminal

I’m writing task for reaction time to audio stimuli. Listeners hear an audio file and the press up-arrow, down-arrow, left-arrow, right-arrow, space, or return. Then we go on to the next audio file, and so on for hundreds of these. I’m not using special keys, just the built-in keyboard on listeners’ own Macs. The tool is launched from terminal.

Problem: Every time the listener types a key, it gets echoed back to Terminal. For example the “down-arrow” key is echoed as “^[[B”. So by the time the task is finished, the screen is full of these random-looking characters, which is quite off-putting and non-professional.

How do I suppress this echo? Specifically, while the python program is running, if a user types a key I do not want anything to be typed onto the Terminal screen.

Here’s my relevant code:

from psychopy import prefs
prefs.hardware['audioLib'] = ['PTB']
# Prepare to use the right audio library backend before importing sound
# And correct latency mode
latencyMode = 4 # For PTB. 0, 1, 2, 3, 4
prefs.hardware['audioLatencyMode'] = [str(latencyMode)]
# Now that the prefs are set, we will import the correct version of sound
from psychopy.sound.backend_ptb import SoundPTB
from psychopy.hardware import keyboard
kb = keyboard.Keyboard() # Set up a keyboard device

audio = SoundPTB("myAudio.wav")
audio.play()
keys = kb.waitKeys(keyList = ["up", "down", "left", "right", "space", "return"],
                               waitRelease=False)
audio.stop()

I tried issuing “stty -echo” on Terminal before launching the tool, but the didn’t suppress the key echoes.

Any suggestions?

In GNU/Linux systems you could try to open /dev/input/event* devices and read the input events directly. This way you bypass the terminal emulator and have more control over the input.

But an easier way would be use to “pynput” python library. This will take care of all the details of reading these files and providing the input for you. It is also cross-platform. So I recommend this approach.