From 4ae815c1422cea9a45e82e6059d2328e6f100ccc Mon Sep 17 00:00:00 2001 From: Edern Deneuville Date: Fri, 11 Sep 2026 11:59:37 +0200 Subject: [PATCH] Initial ai typewriter implementation --- src/ai_typewriter/clipboard_capture.py | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/ai_typewriter/clipboard_capture.py diff --git a/src/ai_typewriter/clipboard_capture.py b/src/ai_typewriter/clipboard_capture.py new file mode 100644 index 0000000..b12ea37 --- /dev/null +++ b/src/ai_typewriter/clipboard_capture.py @@ -0,0 +1,29 @@ +from __future__ import annotations + +import time + +import keyboard +import pyperclip + + +def capture_selection(copy_wait_seconds: float = 1.0, restore_clipboard: bool = True) -> str: + previous = pyperclip.paste() + keyboard.send("ctrl+c") + + deadline = time.monotonic() + max(copy_wait_seconds, 0.05) + captured = previous + while time.monotonic() < deadline: + current = pyperclip.paste() + if current != previous: + captured = current + break + time.sleep(0.03) + else: + captured = pyperclip.paste() + + if restore_clipboard: + try: + pyperclip.copy(previous) + except pyperclip.PyperclipException: + pass + return captured or ""