; ClipboardGuard.ahk — Sauvegarde / restauration sûre du presse-papiers (architecture §11.3). ; Règle : on ne restaure que si le presse-papiers contient encore exactement ce qu'on y a mis ; ; si l'utilisateur a copié autre chose entre-temps, on abandonne sans rien écraser. class ClipboardGuard { Saved := "" ; ClipboardAll sauvegardé HasSaved := false InjectedText := "" Save() { try this.Saved := ClipboardAll() this.HasSaved := true } ClearSave() { this.Saved := "" this.HasSaved := false this.InjectedText := "" } ; À appeler après l'injection (via minuterie) : restauration conditionnelle. RestoreIfUnchanged() { if (!this.HasSaved) return current := "" try current := A_Clipboard if (current = this.InjectedText) { try A_Clipboard := this.Saved } this.ClearSave() } ; Mode sensitive (§18, point 6) : vider plutôt que restaurer. WipeIfUnchanged() { if (!this.HasSaved) return current := "" try current := A_Clipboard if (current = this.InjectedText) A_Clipboard := "" this.ClearSave() } ; Capture best-effort du texte sélectionné dans l'application active (§11.2). ; Sauvegarde le presse-papiers, simule Ctrl+C, lit, restaure immédiatement. ; Retourne "" si rien n'est sélectionné ou si la capture échoue — sans jamais lever d'erreur. static CaptureSelection() { saved := "" try saved := ClipboardAll() result := "" try { A_Clipboard := "" Send("^c") if ClipWait(0.25) { if DllCall("IsClipboardFormatAvailable", "UInt", 13) ; CF_UNICODETEXT result := A_Clipboard } } try { if (saved != "") A_Clipboard := saved else A_Clipboard := "" } return result } static ClipboardText() { txt := "" try { if DllCall("IsClipboardFormatAvailable", "UInt", 13) txt := A_Clipboard } return txt } }