8

I've been trying to figure out how to insert/expand long text faster. The current keystroke method I'm using is quite time consuming and therefore something I would rather avoid.

Right now I am using the following method:

::abc::all bad cats

Or for longer text:

::li::
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

However the second method is a little slow.

Any suggestions for how I can avoid this slow expansion method? Perhaps by using the clipboard to copy and paste from the AHK script?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Shezan Kazi
  • 4,471
  • 3
  • 14
  • 27

3 Answers3

15

Try this:

::li::
ClipSaved := ClipboardAll ; save the entire clipboard to the variable ClipSaved
clipboard := ""           ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
clipboard =               ; copy this text:
(
Lorem ipsum dolor ...
line2
..
)
ClipWait, 2              ; wait max. 2 seconds for the clipboard to contain data. 
if (!ErrorLevel)         ; If NOT ErrorLevel, ClipWait found data on the clipboard
    Send, ^v             ; paste the text
Sleep, 300               ; don't change clipboard while pasting! (Sleep > 0)
clipboard := ClipSaved   ; restore original clipboard
VarSetCapacity(ClipSaved, 0) ; free the memory in case the clipboard was very large.
return

If you often have to send such a complex or long text, you can create a function, for not repeating the whole code every time:

::li::
my_text =
(
Lorem ipsum dolor ...
line2
...
)
Send(my_text)
return


Send(text){
    ClipSaved := ClipboardAll
    clipboard := ""
    clipboard := text
    ClipWait, 1
    If (!ErrorLevel)
        Send, ^v        
    Sleep, 300
    clipboard := ClipSaved
    VarSetCapacity(ClipSaved, 0)
}

See ClipboardAll and ClipWait

user3419297
  • 9,537
  • 2
  • 15
  • 24
  • 1
    Is there a reason you have `ClipWait` commented out? [AHK's documentation](https://autohotkey.com/docs/misc/Clipboard.htm) says it increases script reliability. – Stevoisiak Aug 23 '17 at 17:45
3

Another Way to send Quickly long text with autohotkey Scripting languages is,

if you put all the text first to the Windows Registry Memory.

then you can Read it With [Registry Ram Memory Speed] to the [Clipboard Memory] paste it and it is done.

You can try this code:

Example1.ahk:

; [^ = Ctrl] [+ = Shift] [! = Alt] [# = Win]
#SingleInstance Force

;Variant 1
;put any clipboard text into variable a1
;send ^c
;a1=%clipboard%

;Variant 2
;or put any Variable to variable a1
;a1 := "Any Text"

;Variant 3
;or put any File text to variable a1
FileRead, a1, C:\My File1.txt
FileRead, a2, C:\My File2.txt

;Write the variable's To Registry - KeyHintText,value1 to value2
RegWrite, REG_SZ, HKEY_CURRENT_USER, software\KeyHintText,value1,%a1%
RegWrite, REG_SZ, HKEY_CURRENT_USER, software\KeyHintText,value2,%a2%

:*:abc:: 
clipboard := a1
send ^v
return

:*:def:: 
clipboard := a2
send ^v
return

Note - :*:abc:: = (you can type abc without Space) - ::abc:: = (you can type abc with Space)

If you use Windows Registry then the pros are :

1 - If the Value1 Text In the Registry is CHANGED, you use it with the same Hotkey :*:abc::

:*:abc:: 
RegRead, clipboard, HKEY_CURRENT_USER,software\KeyHintText,value1
send ^v
return

2 - If you Restart the Computer, all the Text is automatic Saved to the Ram Memory.

You can then only use this Code

example2.ahk

; [^ = Ctrl] [+ = Shift] [! = Alt] [# = Win]
#SingleInstance Force

;read the Variable's From Windows Registry
RegRead, a1, HKEY_CURRENT_USER,software\KeyHintText,value1 
RegRead, a2, HKEY_CURRENT_USER,software\KeyHintText,value2

:*:abc:: 
clipboard := a1
send ^v
return

:*:def:: 
clipboard := a2
send ^v
return

Tip: I use it With Buttoncommander Software you can then make on the Windows Desktop a set of Clickable Pictures (toolbars), you can replace for example the abc text into Pictures, if you push these Images With your Mouse or touch device it will Execute (native) the Autohotkey Command Codes.

stevecody
  • 658
  • 1
  • 6
  • 18
  • Is there a reason this isn't marked as the correct answer? What a wonderfully thorough and well-written response. This covers so many scenarios and has a lot of useful information for new (and even moderate) users. +1 for sure Edit: If that's the kind of responses you give, you're more than welcome to visit us over at the /r/AutoHotkey subreddit on Reddit.com. We're always looking for new, well informed and positive contributors to help the masses. – GroggyOtter Aug 20 '18 at 03:36
2
::li::  
text =
(
Line1
Line2
...
)
; IfWinActive, ahk_group textEditors ; create a group in the auto execute section
SendInput, %text%                      ;  SendInput is faster and more reliable
return

or

::li::
; IfWinActive, ahk_group textEditors
SendInput,
(
Line1
Line2
...
)
return
user3419297
  • 9,537
  • 2
  • 15
  • 24
  • Thanks a lot! Somehow though, I doesn't seem to be faster than the standard method I used before. Are there any adjustments I need to make to the agh script? – Shezan Kazi Dec 10 '14 at 09:45
  • I can confirm, SendInput's performance is bad for long text, sometimes the characters got mixed up. – kntx Dec 02 '21 at 11:11