Advertisement
Python253

keystroke_recorder

May 22nd, 2024
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.29 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: keystroke_recorder.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script monitors and logs keystrokes to a text file.
  10.    - It uses the pynput library to listen for keyboard events and logs each keystroke with a timestamp.
  11.    - The script prompts the user to start and stop monitoring, and to exit gracefully.
  12.  
  13. Functions:
  14.    - on_press(key):
  15.        Logs each key press to a text file with a timestamp.
  16.    - monitor_input():
  17.        Prompts the user to start and stop monitoring, and to exit the program.
  18.  
  19. Requirements:
  20.    - Python 3.x
  21.    - Pynput Module (install using `pip install pynput`)
  22.  
  23. Usage:
  24.    - Run the script using `python keystroke_recorder.py`.
  25.    - Follow the prompts in the terminal to start monitoring, stop monitoring, and exit the program.
  26.  
  27. Example Expected Output:
  28.  
  29.    2020-04-20 18:16:26.210462: Key.enter
  30.    2020-04-20 18:16:35.858730: t
  31.    2020-04-20 18:16:36.041972: e
  32.    2020-04-20 18:16:36.265978: s
  33.    2020-04-20 18:16:36.506444: t
  34.    2020-04-20 18:16:37.178961: Key.space
  35.    2020-04-20 18:16:37.686928: k
  36.    2020-04-20 18:16:37.836686: e
  37.    2020-04-20 18:16:38.111881: y
  38.    2020-04-20 18:16:38.363728: s
  39.    2020-04-20 18:16:38.565703: t
  40.    2020-04-20 18:16:38.721222: r
  41.    2020-04-20 18:16:39.167719: o
  42.    2020-04-20 18:16:39.399612: k
  43.    2020-04-20 18:16:39.618355: e
  44.    2020-04-20 18:16:39.894371: Key.space
  45.    2020-04-20 18:16:40.355300: r
  46.    2020-04-20 18:16:40.572588: e
  47.    2020-04-20 18:16:40.881969: c
  48.    2020-04-20 18:16:41.117424: o
  49.    2020-04-20 18:16:41.482916: r
  50.    2020-04-20 18:16:41.766971: d
  51.    2020-04-20 18:16:41.976781: e
  52.    2020-04-20 18:16:42.208465: r
  53.  
  54. Additional Notes:
  55.    - Ensure that you have the necessary permissions to run the script.
  56.    - The script logs all keystrokes, including special keys like Enter, Space, and others.
  57. """
  58.  
  59. from pynput.keyboard import Key, Listener
  60. import datetime
  61. import threading
  62. import os
  63.  
  64. # Set up the text file
  65. txt_file = "keystrokes.txt"
  66. stop_flag = threading.Event()
  67.  
  68. def on_press(key):
  69.     """
  70.    Logs each key press to a text file with a timestamp.
  71.  
  72.    Args:
  73.        key: The key that was pressed.
  74.    """
  75.     with open(txt_file, "a", encoding="utf-8") as f:
  76.         if hasattr(key, "char"):
  77.             f.write(f"{datetime.datetime.now()}: {key.char}\n")
  78.         else:
  79.             f.write(f"{datetime.datetime.now()}: {key}\n")
  80.     if stop_flag.is_set():
  81.         return False  # Stop the listener
  82.  
  83. def monitor_input():
  84.     """
  85.    Prompts the user to start and stop monitoring, and to exit the program.
  86.    """
  87.     input("Press Enter to start monitoring...\n")
  88.  
  89.     print("Monitoring keystrokes...\n")
  90.  
  91.     input("Press Enter to stop...\n")
  92.  
  93.     print("Saving log to:")
  94.     print(f"- {os.getcwd()}/{txt_file}\n")
  95.     stop_flag.set()
  96.  
  97.     print("Keystrokes saved to:")
  98.     print(f"- {os.path.abspath(txt_file)}\n")
  99.  
  100.     input("Press Enter to exit...\n")
  101.  
  102. # Create a listener thread
  103. listener_thread = threading.Thread(target=Listener(on_press=on_press).start)
  104.  
  105. # Start the listener thread
  106. listener_thread.start()
  107.  
  108. # Monitor user input
  109. monitor_input()
  110.  
  111. # Wait for the listener thread to finish
  112. listener_thread.join()
  113.  
  114. print("Program exited... Goodbye!")
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement