Random text on key press Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
fona
Posts: 65
Joined: 07 Apr 2022, 06:43

Random text on key press

Post by fona » 01 Apr 2023, 12:00

Dear friends,
I would like to get some random text on key press but this code below doesn't work for some reason.

Code: Select all

Var1 := "Hello"
Var2 := "{Hi|hallo} i want to talk with you"
Var3 := "{Hallooo|Hey} I am availible"
Var4 := "Greetings"
Var5 := "Bla bla"

F1::
  Random, OutputVar , 1, 5
  Sendraw % Var%OutputVar%
Return
[Mod edit: Moved to AHK v1 help, based on posted code.]

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Random text on key press

Post by swagfag » 01 Apr 2023, 12:36

looks like valid v1 code
...and invalid v2 code

Rohwedder
Posts: 7622
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Random text on key press

Post by Rohwedder » 01 Apr 2023, 13:17

Hallo,
try:

Code: Select all

Var1 := "Hello"
Var2 := "{Hi|hallo|hallo you} i want to {talk with|kill} you"
Var3 := "{Hallooo|Hey} I am availible {now|later|never}"
Var4 := "Greetings"
Var5 := "Bla bla"
F1::
Random, OutputVar, 1, 5
Text := Var%OutputVar%
While, RegExMatch(Text, "{([^}]+)}", EitherOr)
{
	EitherOr2 := StrSplit(EitherOr1, "|")
	Random, No, 1, EitherOr2.MaxIndex()
	Text := StrReplace(Text, EitherOr, EitherOr2[No])
}
Send % Text "`n"
Return

fona
Posts: 65
Joined: 07 Apr 2022, 06:43

Re: Random text on key press

Post by fona » 01 Apr 2023, 19:30

Rohwedder wrote:
01 Apr 2023, 13:17
Hallo,
try:

Code: Select all

Var1 := "Hello"
Var2 := "{Hi|hallo|hallo you} i want to {talk with|kill} you"
Var3 := "{Hallooo|Hey} I am availible {now|later|never}"
Var4 := "Greetings"
Var5 := "Bla bla"
F1::
Random, OutputVar, 1, 5
Text := Var%OutputVar%
While, RegExMatch(Text, "{([^}]+)}", EitherOr)
{
	EitherOr2 := StrSplit(EitherOr1, "|")
	Random, No, 1, EitherOr2.MaxIndex()
	Text := StrReplace(Text, EitherOr, EitherOr2[No])
}
Send % Text "`n"
Return
Doesn't work :(

cat dog fox war
Posts: 38
Joined: 15 Mar 2023, 10:18

Re: Random text on key press

Post by cat dog fox war » 01 Apr 2023, 21:27

Your original code, I tested in Notepad is no problem.
Do you expect to output to a special window?
Such as game/administrator privilege window/cmd, etc.

Rohwedder
Posts: 7622
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Random text on key press  Topic is solved

Post by Rohwedder » 02 Apr 2023, 03:37

Then perhaps slower?:

Code: Select all

#SingleInstance, Force
; If !A_IsAdmin
	; Try Run *RunAs "%A_ScriptFullPath%"
Var1 := "Hello"
Var2 := "{Hi|hallo|hallo you} i want to {talk with|kill} you"
Var3 := "{Hallooo|Hey} I am availible {now|later|never}"
Var4 := "Greetings"
Var5 := "Bla bla"
F1::
Random, OutputVar, 1, 5
Text := Var%OutputVar%
While, RegExMatch(Text, "{([^}]+)}", EitherOr)
{
	EitherOr2 := StrSplit(EitherOr1, "|")
	Random, No, 1, EitherOr2.MaxIndex()
	Text := StrReplace(Text, EitherOr, EitherOr2[No],, 1)
}
Loop, Parse, Text
	RandomSendInput(A_LoopField)
SendInput, `n
Return
RandomSendInput(Char, TMin:=30, TMax:=80)
{ ; Mainly by TheBeginner https://www.autohotkey.com/boards/viewtopic.php?f=13&t=90974
    val := DllCall("VkKeyScanA", "char", asc(Char)), low := val & 0xFF, high := val >> 8
	, shift := high & 1, ctrl := high & 2 != 0, alt := high & 4 != 0    
    SendInput,% (shift?"{LShift down}":"") (ctrl?"{LCtrl down}":"") (alt?"{RAlt down}":"")
    SendInput,% (vk:="{vk" Format("{:x}", low)) " Down}"
	Random, Sleep, TMin, TMax
	Sleep, Sleep
    SendInput,% vk " Up}" (shift?"{LShift up}":"") (ctrl?"{LCtrl up}":"") (alt?"{RAlt up}":"")
	Random, Sleep, TMin, TMax
	Sleep, Sleep
}

fona
Posts: 65
Joined: 07 Apr 2022, 06:43

Re: Random text on key press

Post by fona » 02 Apr 2023, 09:09

Rohwedder wrote:
02 Apr 2023, 03:37
Then perhaps slower?:

Code: Select all

#SingleInstance, Force
; If !A_IsAdmin
	; Try Run *RunAs "%A_ScriptFullPath%"
Var1 := "Hello"
Var2 := "{Hi|hallo|hallo you} i want to {talk with|kill} you"
Var3 := "{Hallooo|Hey} I am availible {now|later|never}"
Var4 := "Greetings"
Var5 := "Bla bla"
F1::
Random, OutputVar, 1, 5
Text := Var%OutputVar%
While, RegExMatch(Text, "{([^}]+)}", EitherOr)
{
	EitherOr2 := StrSplit(EitherOr1, "|")
	Random, No, 1, EitherOr2.MaxIndex()
	Text := StrReplace(Text, EitherOr, EitherOr2[No],, 1)
}
Loop, Parse, Text
	RandomSendInput(A_LoopField)
SendInput, `n
Return
RandomSendInput(Char, TMin:=30, TMax:=80)
{ ; Mainly by TheBeginner https://www.autohotkey.com/boards/viewtopic.php?f=13&t=90974
    val := DllCall("VkKeyScanA", "char", asc(Char)), low := val & 0xFF, high := val >> 8
	, shift := high & 1, ctrl := high & 2 != 0, alt := high & 4 != 0    
    SendInput,% (shift?"{LShift down}":"") (ctrl?"{LCtrl down}":"") (alt?"{RAlt down}":"")
    SendInput,% (vk:="{vk" Format("{:x}", low)) " Down}"
	Random, Sleep, TMin, TMax
	Sleep, Sleep
    SendInput,% vk " Up}" (shift?"{LShift up}":"") (ctrl?"{LCtrl up}":"") (alt?"{RAlt up}":"")
	Random, Sleep, TMin, TMax
	Sleep, Sleep
}
Thank you so much Rohwedder

Post Reply

Return to “Ask for Help (v1)”