Writing a String - Beginner Topic is solved

Ask gaming related questions (AHK v1.1 and older)
autofriend
Posts: 12
Joined: 17 Oct 2021, 20:57

Writing a String - Beginner

Post by autofriend » 17 Oct 2021, 21:02

Hello All,

This is my first time writing my own script for a personal project. I'm not really familiar with any other scripting or coding languages (tried learning python once upon a time). This is the best way to learn for me, when I am passionate about something and want to automate things in my life using scripts.

For the current project that I'm working on, I'm trying to write a script where it will send key strokes periodically. For example (in my own language):

!r::
Send, 0 then, m, then 8
wait 2 seconds...

Send, i, then, 0, then 8
wait 2 seconds...

Send, i, then, 0, then 8
in a continuous loop with different commands until I complete the loop.

I know I really dumbed it down but would really appreciate any guidance on this.

Thanks in advance!

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Writing a String - Beginner

Post by mikeyww » 17 Oct 2021, 21:20

Code: Select all

#MaxThreadsPerHotkey 2
!r::
on := !on
While on {
 Send 0m8
 Sleep, 2000 * on
 If on
  Send i08
 Sleep, 2000 * on
 If on
  Send i08
}
Return

autofriend
Posts: 12
Joined: 17 Oct 2021, 20:57

Re: Writing a String - Beginner

Post by autofriend » 17 Oct 2021, 21:38

i08Thanks so much, i080m8i08i080m8i08 loolmmi08,,, i08how do ii080m8 end this commani08di080m8

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Writing a String - Beginner  Topic is solved

Post by mikeyww » 17 Oct 2021, 21:48

Alt+R is a toggle that will start or stop.

autofriend
Posts: 12
Joined: 17 Oct 2021, 20:57

Re: Writing a String - Beginner

Post by autofriend » 17 Oct 2021, 21:49

lol, so i just restarted my computer and it stopped. I think i just need to figure out how to toggle this command off, and between the 0m8, it does it a little bit too quickly, any way I could slow that down?

Thanks

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Writing a String - Beginner

Post by mikeyww » 17 Oct 2021, 21:51

Code: Select all

#MaxThreadsPerHotkey 2
!r::
SetKeyDelay, 100, 25
on := !on
While on {
 Send 0m8
 Sleep, 2000 * on
 If on
  Send i08
 Sleep, 2000 * on
 If on
  Send i08
}
Return

autofriend
Posts: 12
Joined: 17 Oct 2021, 20:57

Re: Writing a String - Beginner

Post by autofriend » 17 Oct 2021, 21:55

This is great, thanks a lot Mikey. I'll let you know if I have any other questions. I'm really starting to like coding

autofriend
Posts: 12
Joined: 17 Oct 2021, 20:57

Re: Writing a String - Beginner

Post by autofriend » 17 Oct 2021, 22:01

quick follow up question. After the initial command of "send 0m8", I don't want it to repeat it afterwards. I would only want it to just continue the pattern of i08. Is there a way to set it that the initial command of "send 0m8" Isn't done again after I toggle the command on?

autofriend
Posts: 12
Joined: 17 Oct 2021, 20:57

Re: Writing a String - Beginner

Post by autofriend » 17 Oct 2021, 22:14

This is what I have currently. Instead of it looping back to my original "Send 0m, I'd like it to just keep doing the commands set underneath it in order.

Code: Select all

#MaxThreadsPerHotkey 2
!r::
on := !on
While on {
 Send 0m ; first command
SetKeyDelay, 5000, 200, 200, 50
 Sleep, 2000 * on
 If on
  Send 8o0m ; second command
 Sleep, 2000 * on
 If on
  Send 8o0m ; third command
If on
  Send 8o0m ;fourth command
If on
  Send 8o0m ; fifth command
If on
  Send 8k0m ; sixth command (I plan on adding more and more, but eventually, it will go back to the second command and continue that cycle.)
}
Return
[Mod edit: [code][/code] tags added.]

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Writing a String - Beginner

Post by mikeyww » 17 Oct 2021, 22:15

Here is an example from the earlier post. You can adjust as needed.

Code: Select all

!r::
SetKeyDelay, 100, 25              ; Wait 25 ms after key down, 100 ms after key up
Send % (on := !on) ? "0m8" : ""   ; Toggle "on" flag; if true, send 0m8
SetTimer, Go, % on ? 2000 : "Off" ; If toggled on, set timer; otherwise, disable it
Return
Go:                               ; Timed routine
SetKeyDelay, 100, 25
Send i08
Return
Click the green commands in the posted script to learn more about them.

Alternative:

Code: Select all

#MaxThreadsPerHotkey 2
Global on
wait = 2000
!r::
SetKeyDelay, 100, 25
on := !on, keys("0m8", wait)
While on
 keys("i08", wait), keys("i08", wait), keys("ik8", wait)
Return

keys(str, wait) {
 Send % on ? str : ""
 Sleep, wait * on
}
With this type of script, there is a chance that AHK will send Alt up after you send it down but before you press R.

autofriend
Posts: 12
Joined: 17 Oct 2021, 20:57

Re: Writing a String - Beginner

Post by autofriend » 17 Oct 2021, 22:39

Thanks Mikey, for the SetKeyDelay function, it doesn't seem to be working for me. I've tried to input higher numbers like 5000 for 5 seconds, but id doesn't seem to make a difference. Any insight on this?

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Writing a String - Beginner

Post by mikeyww » 17 Oct 2021, 22:47

I wouldn't recommend it for long waits like that-- it's uninterruptible-- but your script did work.

Code: Select all

#MaxThreadsPerHotkey 2
!r::
If on := !on {
 Send 0m ; first command
 Sleep, 2000
}
SetKeyDelay, 5000, 200
While on
 Send 8o0m ; second command
Return
This differs from the earlier scripts: the SetKeyDelay occurs after every key.

Get the syntax right; you can't make up your own. See the documentation.

autofriend
Posts: 12
Joined: 17 Oct 2021, 20:57

Re: Writing a String - Beginner

Post by autofriend » 17 Oct 2021, 22:52

The wait =2000 or sleep,2000 command does work. My concern was the 1st or second command in general. For example, the 8o0m is clicked very quickly, one after the other. The key delay doesn't offer much of a delay between each of those keys clicked for me. Is there something I'm doing wrong?

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Writing a String - Beginner

Post by mikeyww » 17 Oct 2021, 22:53

Use Notepad to test the script that I posted. Post your revised script if needed. Exit other scripts if any are running. Save and reload your script.
Last edited by mikeyww on 17 Oct 2021, 22:55, edited 1 time in total.

autofriend
Posts: 12
Joined: 17 Oct 2021, 20:57

Re: Writing a String - Beginner

Post by autofriend » 17 Oct 2021, 22:54

I've just tested on notepad. the first and second commands from the script pasted below looks like it's getting copied and pasted instead of clicked as someone naturally would

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Writing a String - Beginner

Post by mikeyww » 17 Oct 2021, 22:56

Pasted below?
Last edited by mikeyww on 17 Oct 2021, 22:56, edited 2 times in total.

autofriend
Posts: 12
Joined: 17 Oct 2021, 20:57

Re: Writing a String - Beginner

Post by autofriend » 17 Oct 2021, 22:56

Code: Select all

#MaxThreadsPerHotkey 2
!r::
If on := !on {
 Send 0m ; first command
 Sleep, 2000
}
SetKeyDelay, 5000, 200
While on
 Send 8o0m ; second command
Return
[Mod edit: [code][/code] tags added.]

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Writing a String - Beginner

Post by mikeyww » 17 Oct 2021, 22:57

When I run it, it does what it is programmed to do: sends 0m, and then the 8o0m, with a 5-second delay after every key. I don't know what "looks like it's getting copied and pasted" means. When you use AHK to send keys, it sends the keys as if you typed them.
Last edited by mikeyww on 17 Oct 2021, 22:59, edited 1 time in total.

autofriend
Posts: 12
Joined: 17 Oct 2021, 20:57

Re: Writing a String - Beginner

Post by autofriend » 17 Oct 2021, 22:59

Now it does for me too, I think originally when i first got AhK, it had other commands/scripts at the top of the page.

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Writing a String - Beginner

Post by mikeyww » 17 Oct 2021, 23:00

Well, OK,.... your script will do whatever it says to do! :)

Suggestion: until you get the hang of it, when you post your script, post your script-- which is the whole thing (file).

Post Reply

Return to “Gaming Help (v1)”