feat: add Win32 inserter backend with clipboard + Ctrl+V
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
"""Text-Einfügung via pynput Clipboard + Ctrl+V (Windows)."""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from pynput.keyboard import Controller, Key
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
keyboard_controller = Controller()
|
||||
|
||||
PASTE_DELAY = 0.2
|
||||
|
||||
|
||||
class Win32Inserter:
|
||||
def _get_clipboard(self) -> str:
|
||||
"""Liest den aktuellen Clipboard-Inhalt (Text)."""
|
||||
import win32clipboard
|
||||
try:
|
||||
win32clipboard.OpenClipboard()
|
||||
try:
|
||||
data = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT)
|
||||
return data
|
||||
except TypeError:
|
||||
return ""
|
||||
finally:
|
||||
win32clipboard.CloseClipboard()
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
def _set_clipboard(self, text: str) -> None:
|
||||
"""Setzt den Clipboard-Inhalt."""
|
||||
import win32clipboard
|
||||
win32clipboard.OpenClipboard()
|
||||
try:
|
||||
win32clipboard.EmptyClipboard()
|
||||
win32clipboard.SetClipboardText(text, win32clipboard.CF_UNICODETEXT)
|
||||
finally:
|
||||
win32clipboard.CloseClipboard()
|
||||
|
||||
async def insert(self, text: str) -> None:
|
||||
"""Fügt Text ins aktive Fenster ein via Clipboard + Ctrl+V."""
|
||||
if not text:
|
||||
return
|
||||
|
||||
old_clipboard = await asyncio.to_thread(self._get_clipboard)
|
||||
|
||||
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.sleep(PASTE_DELAY)
|
||||
finally:
|
||||
await asyncio.to_thread(self._set_clipboard, old_clipboard)
|
||||
|
||||
logger.info("Text eingefügt (%d Zeichen)", len(text))
|
||||
Reference in New Issue
Block a user