ClipSend() - Send text quickly with clipboard

Post your working scripts, libraries and tools for AHK v1.1 and older
william_ahk
Posts: 486
Joined: 03 Dec 2018, 20:02

ClipSend() - Send text quickly with clipboard

Post by william_ahk » 25 Mar 2022, 07:36

Function:

Code: Select all

ClipSend(Text, PS := "") {
    ClipSave := ClipboardAll
    Clipboard := Text
    Send ^v
    while DllCall("GetOpenClipboardWindow")
        sleep -1
    Clipboard := ClipSave
    if PS
        Send % PS
}
There were previous libraries of the same functionality but they all used SetTimer to restore the clipboard, which causes Ctrl+V to send the saved clipboard content on occasion. Recently I discovered that one can use GetOpenClipboardWindow to determine whether the system clipboard is currently being used. So I adopted the function to block clipboard restoration before pasting is finished.

Example usage:

Code: Select all

!l::ClipSend("a very loooooooooooooooooooooooooooooooong text")

Code: Select all

; You can also specify words or keys to press right after the content is pasted as the second argument for convenience.
:X:dqs::ClipSend("document.querySelector("""")", "{Left 2}")
:X:cslg::ClipSend("console.log()", "{Left 1}")

Code: Select all

html_boilerplate =
(
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

</body>
</html>
)

:X:htmlbp::ClipSend(html_boilerplate, "{Up 2}")


gibbons6546
Posts: 31
Joined: 07 Apr 2020, 11:53

Re: ClipSend() - Send text quickly with clipboard

Post by gibbons6546 » 26 Mar 2022, 16:27

Thanks. Will try to implement this.

Post Reply

Return to “Scripts and Functions (v1)”