fix: add pywin32 dep, move Controller into class, wrap keyboard in to_thread

This commit is contained in:
Vitali Graf
2026-04-08 10:43:14 +02:00
parent 670ffabb1f
commit b94c58d628
4 changed files with 34 additions and 15 deletions
+11 -8
View File
@@ -7,12 +7,13 @@ from pynput.keyboard import Controller, Key
logger = logging.getLogger(__name__)
keyboard_controller = Controller()
PASTE_DELAY = 0.2
class Win32Inserter:
def __init__(self):
self._keyboard = Controller()
def _get_clipboard(self) -> str:
"""Liest den aktuellen Clipboard-Inhalt (Text)."""
import win32clipboard
@@ -38,6 +39,13 @@ class Win32Inserter:
finally:
win32clipboard.CloseClipboard()
def _send_paste(self) -> None:
"""Simuliert Ctrl+V."""
self._keyboard.press(Key.ctrl)
self._keyboard.press('v')
self._keyboard.release('v')
self._keyboard.release(Key.ctrl)
async def insert(self, text: str) -> None:
"""Fügt Text ins aktive Fenster ein via Clipboard + Ctrl+V."""
if not text:
@@ -48,12 +56,7 @@ class Win32Inserter:
try:
await asyncio.to_thread(self._set_clipboard, text)
await asyncio.sleep(0.05)
keyboard_controller.press(Key.ctrl)
keyboard_controller.press('v')
keyboard_controller.release('v')
keyboard_controller.release(Key.ctrl)
await asyncio.to_thread(self._send_paste)
await asyncio.sleep(PASTE_DELAY)
finally:
await asyncio.to_thread(self._set_clipboard, old_clipboard)