how to replace multiple ::key:: to a copy-paste function ?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
S_N
Posts: 10
Joined: 19 May 2019, 13:38

how to replace multiple ::key:: to a copy-paste function ?

06 Dec 2021, 11:08

i have many hotkeys which type text letter by letter...
sorry... i just recently discovered clipboard ... :P

is there a way to re-script my code to make it simpler ?
i tried replacing a few of the codes, but its quite painful/time-consuming to do for all the lines...

example

Code: Select all

::^F3::alongstringoftext
::shortcut::
SendInput `
(
alongparagraph
withmultiple
lines
)
return
my current... painful to apply solution...:

Code: Select all

::^F3::
t_str=
(
alongstringoftext
)
return

::shortcut::
t_str=
(
alongparagraph
withmultiple
lines
)
copypasta(t_str)
return

copypasta(t_str)
{
clipboard:=t_str
send ^v
clipboard:=""
return
}
Last edited by Masonjar13 on 06 Dec 2021, 11:10, edited 1 time in total.
Reason: Added code tags.
User avatar
mikeyww
Posts: 26936
Joined: 09 Sep 2014, 18:38

Re: how to replace multiple ::key:: to a copy-paste function ?

06 Dec 2021, 11:17

If you are using a standard edit control, you can also use the Control, EditPaste command.

You could put all of your text into a separate text file that includes delimiters. Your script could then just read the file. It's just another way to manage your text, neither better nor worse in the final outcome. An INI file might even be handy. IniRead

Code: Select all

Global ini := StrReplace(A_ScriptFullPath, ".ahk", ".ini")
IniWrite, alongparagraph|withmultiple|lines, %ini%, Section1, par1
F3::paste("Section1", "par1")

paste(section, key) {
 IniRead, ttext, %ini%, %section%, %key%
 Clipboard := "", Clipboard := StrReplace(ttext, "|", "`n")
 ClipWait, 0
 If ErrorLevel
  MsgBox, 48, Error, An error occurred while waiting for the clipboard.
 Else Send ^v
}
S_N
Posts: 10
Joined: 19 May 2019, 13:38

Re: how to replace multiple ::key:: to a copy-paste function ?

07 Dec 2021, 18:46

ok i got to this place , where hotkeys seem to be assigned,
but i'm unable to pass the %value% outside into the copypasta function
i added global but that only gives me the last line in the definitions list with any hotkey used ...

thank you !

Code: Select all

KEYSARRAY := []
global key
global value

IniFile := "hkdefinitions.ini"   ;-------- hot key definitions list file 

IniRead, hkinireadvar, %IniFile%, hotkeys
KEYSARRAY := Object(StrSplit(hkinireadvar, ["=", "`n"])*) ; make array by splitting using '=' and '`n' as delimiters.
for key,value in KEYSARRAY
  hotkey, %key%, copypasta
return

copypasta(){
 Clipboard := %value%
 ClipWait, 0
 If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
else send ^v
}
S_N
Posts: 10
Joined: 19 May 2019, 13:38

Re: how to replace multiple ::key:: to a copy-paste function ?

07 Dec 2021, 23:11

mikeyww wrote:
07 Dec 2021, 19:41
You can post your INI file here.
Its just test stuff ini

Code: Select all

[hotstrings]
sn1=0123456789
sn2=987654321

[hotkeys]
^f1=Short words
+c=Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam convallis justo ac libero scelerisque, vitae scelerisque libero gravida. Nam malesuada nunc nulla, id ornare massa accumsan at. Curabitur at turpis feugiat, 
F3=mark
User avatar
mikeyww
Posts: 26936
Joined: 09 Sep 2014, 18:38

Re: how to replace multiple ::key:: to a copy-paste function ?

08 Dec 2021, 05:58

Code: Select all

dir       := A_ScriptDir
IniFile   := dir "\hkdefinitions.ini"
KEYSARRAY := {}
IniRead, hkinireadvar, %IniFile%, hotkeys
For each, line in StrSplit(hkinireadvar, "`n") {
 RegExMatch(line, "(.+?)=(.*)", part), KEYSARRAY[part1] := part2
 Hotkey, %part1%, copypasta
}
Return

copypasta:
Clipboard := "", Clipboard := KEYSARRAY[A_ThisHotkey]
ClipWait, 0
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Else Send ^v
Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, Google [Bot], OrangeCat and 140 guests