Putting Text Into the Clipboard

Get help with using AutoHotkey and its commands and hotkeys
Alexander2
Posts: 36
Joined: 27 Apr 2019, 17:38

Putting Text Into the Clipboard

02 Aug 2020, 09:14

Can someone explain how to put specified text into the clipboard when a specified key is pressed? (For example, the text “example example example” when the key Numpad5 is pressed.)
Is it also possible to put the text in a specified font and size? (E.g. Calibri with size 16.)
User avatar
boiler
Posts: 5911
Joined: 21 Dec 2014, 02:44

Re: Putting Text Into the Clipboard

02 Aug 2020, 09:59

Alexander2 wrote: Can someone explain how to put specified text into the clipboard when a specified key is pressed? (For example, the text “example example example” when the key Numpad5 is pressed.)

Code: Select all

Numpad5::Clibboard := "example example example"

Alexander2 wrote: Is it also possible to put the text in a specified font and size? (E.g. Calibri with size 16.)
It wouldn’t be easy. Don’t know how to insert all the formatting that Word or something like that expects.
Alexander2
Posts: 36
Joined: 27 Apr 2019, 17:38

Re: Putting Text Into the Clipboard

03 Aug 2020, 04:21

Thank you for the code. It is easy to put plain text into the clipboard.
teadrinker
Posts: 1818
Joined: 29 Mar 2015, 09:41
Contact:

Re: Putting Text Into the Clipboard

03 Aug 2020, 19:00

Alexander2 wrote: Is it also possible to put the text in a specified font and size? (E.g. Calibri with size 16.)
My try:

Code: Select all

SetFormattedTextToClipboard("AutoHotkey`r`nHello, World!", "Calibri", 16, 0xFF9900)

SetFormattedTextToClipboard(text, font, size, color := 0) {
   static htmlFormat := DllCall("RegisterClipboardFormat", "Str", "HTML Format")
        , CF_UNICODETEXT := 13, LOGPIXELSY := 90, GMEM_MOVEABLE := 0x0002
        , htmlHeader := "Version:0.9`nStartHTML:-1`nEndHTML:-1`nStartFragment:0000000084`nEndFragment:0000000000`n"
        , htmlHeaderSize := 84
   
   hDC := DllCall("GetDC", "Ptr", 0, "Ptr")
   size := Round(DllCall("GetDeviceCaps", "Ptr", hDC, "Int", LOGPIXELSY) * size/72 * 96/A_ScreenDPI)
   DllCall("ReleaseDC", "Ptr", 0, "Ptr", hDC)
   color := Format("#{:06x}", color)
   
   VarSetCapacity(utf8Data, StrPut(text, "UTF-8"))
   StrPut(text, &utf8Data, "UTF-8")
   str := RegExReplace(StrGet(&utf8Data, "cp0"), "\R", "<br>")
   
   html = <span style="font-family: %font%; font-size: %size%px; color: %color%;">%str%</span>
   endFragmentOffset := Format("{:010u}", htmlHeaderSize + StrLen(html))
   htmlHeader := RegExReplace(htmlHeader, "0{10}", endFragmentOffset)
   str := htmlHeader . html
   VarSetCapacity(htmlData, htmlDataLen := StrPut(str, "cp0"))
   StrPut(str, &htmlData, "cp0")
   
   VarSetCapacity(unicodeData, unicodeDataLen := StrPut(text, "UTF-16")*2)
   StrPut(text, &unicodeData, "UTF-16")
   
   DllCall("OpenClipboard", "Ptr", 0)
   DllCall("EmptyClipboard")
   Loop 2 {
      b := A_Index = 1
      len := b ? htmlDataLen : unicodeDataLen
      pData := b ? &htmlData : &unicodeData
      clipFormat := b ? htmlFormat : CF_UNICODETEXT
      hMem := DllCall("GlobalAlloc", "UInt", GMEM_MOVEABLE, "Ptr", len)
      pMem := DllCall("GlobalLock", "Ptr", hMem, "Ptr")
      DllCall("RtlMoveMemory", "Ptr", pMem, "Ptr", pData, "Ptr", len)
      DllCall("SetClipboardData", "UInt", clipFormat, "Ptr", hMem)
      DllCall("GlobalUnlock", "Ptr", hMem)
      DllCall("GlobalFree", "Ptr", hMem)
   }
   DllCall("CloseClipboard")
}
You can paste the Clipboard contents into MS Word to see the result.
Last edited by teadrinker on 04 Aug 2020, 11:30, edited 5 times in total.
User avatar
boiler
Posts: 5911
Joined: 21 Dec 2014, 02:44

Re: Putting Text Into the Clipboard

03 Aug 2020, 19:59

Nice. I had a feeling you’d come up with something. :)
Alexander2
Posts: 36
Joined: 27 Apr 2019, 17:38

Re: Putting Text Into the Clipboard

04 Aug 2020, 04:56

Thank you. The code is very long for the simple formatting command. Do you use a program like Pulover’s Macro Creator which automatically writes AutoHotkey codes for specified commands?
teadrinker
Posts: 1818
Joined: 29 Mar 2015, 09:41
Contact:

Re: Putting Text Into the Clipboard

04 Aug 2020, 06:57

No, this code was written manually.
User avatar
boiler
Posts: 5911
Joined: 21 Dec 2014, 02:44

Re: Putting Text Into the Clipboard

04 Aug 2020, 07:14

Alexander2 wrote: Thank you. The code is very long for the simple formatting command.
In case you are not aware, you don’t repeat that entire code of teadrinker’s every time you want to put formatted text in the clipboard. The function is placed once in your script (out of the way, not in the program flow), and you just make a function call with the specifics whenever you want to use it. The function doesn’t even have to appear in your script if you put it in another file and use #Include or put it in your standard library. When you want to place formatted code in the clipboard, you only need a line like this:

Code: Select all

 SetFormattedTextToClipboard("AutoHotkey`r`nHello, World!", "Calibri", 16, 0xFF9900)
Isn’t that what you had hoped for?
Alexander2 wrote: Do you use a program like Pulover’s Macro Creator which automatically writes AutoHotkey codes for specified commands?
PMC doesn’t write complicated code like what teadrinker showed. PMC is actually not useful for people who understand how to code. It is limiting and inefficient compared to writing AHK code directly. It is intended for people who are not (yet) capable of writing code, not as an aid or tool for developers.
Alexander2
Posts: 36
Joined: 27 Apr 2019, 17:38

Re: Putting Text Into the Clipboard

05 Aug 2020, 06:43

boiler wrote:
04 Aug 2020, 07:14

Code: Select all

 SetFormattedTextToClipboard("AutoHotkey`r`nHello, World!", "Calibri", 16, 0xFF9900)
Isn’t that what you had hoped for?
Yes, I had not expected the code for this simple command to be long.
boiler wrote:
04 Aug 2020, 07:14
PMC doesn’t write complicated code like what teadrinker showed. PMC is actually not useful for people who understand how to code. It is limiting and inefficient compared to writing AHK code directly. It is intended for people who are not (yet) capable of writing code, not as an aid or tool for developers.
I can see that PMC is limited and is for those who do not know have enough programming experience.
I would have never been able to write manually the long code written by teadrinker, because it requires very much coding knowledge.

Return to “Ask For Help”

Who is online

Users browsing this forum: aurelius, Bing [Bot], electrone77 and 56 guests