#Function to monitor clipboard :
def monitor_clipboard():
last_clipboard = ""
while True:
try:
clipboard_content = pyperclip.paste()
if clipboard_content != last_clipboard:
last_clipboard = clipboard_content
print("Copy-paste detected!")
screenshot_path = take_screenshot()
send_email("Copy-Paste Alert", f"A copy-paste action was detected. Clipboard text:\n{clipboard_content}", screenshot_path)
os.remove(screenshot_path)
except Exception as e:
print(f"Error monitoring clipboard: {e}")
time.sleep(1)
Explanation:
This monitor_clipboard function continually monitors the system clipboard for changes, alerts on new content, and takes action accordingly. Here's a detailed breakdown:
Functionality
1. Initialize Last Clipboard State:
o last_clipboard = "": Starts with an empty string to track the previous clipboard content.
2. Monitor Clipboard in a Loop:
o A while True loop continuously checks the clipboard content using pyperclip.paste().
3. Detect Clipboard Changes:
o If the current clipboard content (clipboard_content) differs from last_clipboard, it implies new content has been copied.
4. Take Action on New Content:
o Updates last_clipboard to the new content.
o Prints a message: "Copy-paste detected!".
o Calls take_screenshot() to capture the current screen.
o Sends an email alert using send_email, including the clipboard text and the screenshot.
o Deletes the screenshot file (os.remove(screenshot_path)) to save storage.
5. Error Handling:
o Any exceptions (e.g., issues accessing the clipboard) are caught, and an error message is printed.
6. Pause Between Checks:
o The time.sleep(1) introduces a 1-second delay between clipboard checks, reducing CPU usage.