AHK script to repeatedly type in a certain window?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
TickTok
Posts: 6
Joined: 25 Nov 2023, 09:43

AHK script to repeatedly type in a certain window?

14 Apr 2024, 17:50

Hi all.

I need to create a script that will automatically type/press a key for me at a specific interval regardless of whether or not it's focused. I tried to take a stab at it but I got hopelessly lost because I haven't fiddled with AHK in months and have no prior coding knowledge.

All I need it to do is target a specific window and periodically (every minute or so) just input a key, like E, so that I am consistently typing something in chat overnight. It does not need to press enter otherwise I will just be posting lots of Es. :P

Can anyone help me out with this, if it's not too difficult a script? Thank you.
User avatar
mikeyww
Posts: 27011
Joined: 09 Sep 2014, 18:38

Re: AHK script to repeatedly type in a certain window?

14 Apr 2024, 18:36

Hello,

Ideas are below. This forum also has hundreds or thousands of other scripts that use ControlSend, in case you need more examples.

Code: Select all

#Requires AutoHotkey v2.0
winTitle := 'ahk_exe Notepad.exe'
ctl      := 'RichEditD2DPT1' ; Win 11

#HotIf WinExist(wintitle)
F3:: {
 ToolTip
 SetTimer(go, 60000), go()
 SoundBeep 1500
}
#HotIf

go() {
 Try ControlSend 'a', ctl, winTitle
 Catch as e {
  SetTimer , 0
  ToolTip e.Message
  SoundBeep 1000
 }
}
TickTok
Posts: 6
Joined: 25 Nov 2023, 09:43

Re: AHK script to repeatedly type in a certain window?

17 Apr 2024, 00:35

mikeyww wrote:
14 Apr 2024, 18:36
Hello,

Ideas are below. This forum also has hundreds or thousands of other scripts that use ControlSend, in case you need more examples.

Code: Select all

#Requires AutoHotkey v2.0
winTitle := 'ahk_exe Notepad.exe'
ctl      := 'RichEditD2DPT1' ; Win 11

#HotIf WinExist(wintitle)
F3:: {
 ToolTip
 SetTimer(go, 60000), go()
 SoundBeep 1500
}
#HotIf

go() {
 Try ControlSend 'a', ctl, winTitle
 Catch as e {
  SetTimer , 0
  ToolTip e.Message
  SoundBeep 1000
 }
}
Thank you for your quick reply and sorry for my unsuitably late one. The script you wrote worked excellently for Notepad but unfortunately threw an error when I tried to use it for another application.

For reference, the error it gave me was:

Error: Parameter #1 of SetTimer must not be omitted in this case.
015: Catch as e
015: {
▶ 016: SetTimer(, 0)
017: ToolTip(e.Message)
018: SoundBeep(1000)
It would also pop up with a box stating "Target control not found." I tried to google this error and other people were saying WindowSpy was useful for helping to fix it but I couldn't figure out the problem.

I'm going to read over the page for SetTimer to see if I can figure it out and fix it for myself but I'm not too hopeful. :P I received some inspiring words from a programming friend recently that I'm going to try and take with me moving forward, I'm hoping it will help me learn a bit easier.
User avatar
mikeyww
Posts: 27011
Joined: 09 Sep 2014, 18:38

Re: AHK script to repeatedly type in a certain window?

17 Apr 2024, 05:13

Although the issues stem from your changes to the script, you have not posted your revised script, so I cannot comment about your revisions.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 68 guests