This is an update to the V1 post here. Thanks to @elbitjusticiero for bringing it to my attention that people might still be using it.
Note, please let me know if there are errors in my usage of V2 since a lot of this stuff I learned just now in order to post this!
Code: Select all
; Clip() - Send and Retrieve Text Using the Clipboard
; by berban - updated 6/23/2023
; https://www.autohotkey.com/boards/viewtopic.php?f=83&t=118764
Clip(Text:="", Reselect:="", Restore:=False)
{
Static BackUpClip, Stored := False, LastClip
If Restore {
If (A_Clipboard == LastClip)
A_Clipboard := BackUpClip
BackUpClip := LastClip := Stored := ""
} Else {
If !Stored {
Stored := True
BackUpClip := ClipboardAll() ; ClipboardAll must be on its own line (or does it in v2?)
} Else
SetTimer Clip.Bind(,,True), 0
LongCopy := A_TickCount, A_Clipboard := "", LongCopy -= A_TickCount ; LongCopy gauges the amount of time it takes to empty the clipboard which can predict how long the subsequent clipwait will need
If (Text = "") {
SendInput("^c")
ClipWait LongCopy ? 0.6 : 0.2, True
} Else {
A_Clipboard := LastClip := Text
ClipWait 10
SendInput("^v")
}
SetTimer Clip.Bind(,,True), -700
Sleep 20 ; Short sleep in case Clip() is followed by more keystrokes such as {Enter}
If (Text = "")
Return LastClip := A_Clipboard
Else If ReSelect and ((ReSelect = True) or (StrLen(Text) < 3000)) ; and !(WinActive("ahk_class XLMAIN") or WinActive("ahk_class OpusApp"))
SendInput("{Shift Down}{Left " StrLen(StrReplace(Text, "`r")) "}{Shift Up}")
}
}