Random Space Macro

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
msy92
Posts: 4
Joined: 05 Jun 2021, 13:01

Random Space Macro

12 Jun 2021, 14:37

So I'm trying to put together a random space macro and I have no scripting background. I've found 2 scripts somewhere on the internet and combined them to make the script listed below.
I'm trying to get this script to spam space randomly between 0.04 - 0.08 seconds in-between each space.

I'm using Razer Synapse 2 to record the macro and see the stats.
https://imgur.com/a/CNcFx0H
https://imgur.com/a/XWMyPAq

1st problem = The delay sometimes repeat itself (all numbers should be random!)
2nd problem = I don't know how to add the random delay for when the key is pressed down then goes up.
3rd problem = The delay goes out of the suggested random delay string (sometimes goes up to 0.1 seconds 0r down to 0.00 seconds).

Code:

Code: Select all

ALT::Suspend, Toggle
*~$Space::
Loop

{
GetKeyState, SpaceState, Space, P
If SpaceState = U
break


    Random, r, 40, 80
    sleep r
    Send, {Blind}{Space}
}
return
User avatar
mikeyww
Posts: 26947
Joined: 09 Sep 2014, 18:38

Re: Random Space Macro

12 Jun 2021, 14:54

It would be If (SpaceState = "U").

Code: Select all

$Space::
While GetKeyState("Space", "P") {
 Random, wait, 40, 80
 SetKeyDelay, wait
 Send {Space}
}
Return
msy92
Posts: 4
Joined: 05 Jun 2021, 13:01

Re: Random Space Macro

13 Jun 2021, 05:25

mikeyww wrote:
12 Jun 2021, 14:54
It would be If (SpaceState = "U").

Code: Select all

$Space::
While GetKeyState("Space", "P") {
 Random, wait, 40, 80
 SetKeyDelay, wait
 Send {Space}
}
Return
Thanks for the reply! But upon testing your code I'm getting 0.00 seconds on key down. See the link below:
https://imgur.com/a/5n5kPg2

Many thanks!
User avatar
mikeyww
Posts: 26947
Joined: 09 Sep 2014, 18:38

Re: Random Space Macro

13 Jun 2021, 06:13

Although I do not know that program, from your screenshot, it looks like you are seeing the down and up sequences separately, and indeed, the paired cycles show the correct times as programmed. If you wish, you can also verify that using A_TickCount in AHK.

Code: Select all

$Space::
start := A_TickCount, n := 0
While GetKeyState("Space", "P") {
 Random, wait, 40, 80
 SetKeyDelay, wait
 Send {Space}
 n++
}
end := A_TickCount
MsgBox, 64, Time per space (ms) (N=%n%), % Round((end - start) / n)
Return
msy92
Posts: 4
Joined: 05 Jun 2021, 13:01

Re: Random Space Macro

13 Jun 2021, 07:47

mikeyww wrote:
13 Jun 2021, 06:13
Although I do not know that program, from your screenshot, it looks like you are seeing the down and up sequences separately, and indeed, the paired cycles show the correct times as programmed. If you wish, you can also verify that using A_TickCount in AHK.

Code: Select all

$Space::
start := A_TickCount, n := 0
While GetKeyState("Space", "P") {
 Random, wait, 40, 80
 SetKeyDelay, wait
 Send {Space}
 n++
}
end := A_TickCount
MsgBox, 64, Time per space (ms) (N=%n%), % Round((end - start) / n)
Return
Thanks for your continued response. The time per space measured various but was inside the time string (0.04 - 0.08 seconds) which is good news.
https://imgur.com/a/y9UTlaU

When the results show this does this mean its for key down or key up in terms of duration time?
Many thanks!
User avatar
mikeyww
Posts: 26947
Joined: 09 Sep 2014, 18:38

Re: Random Space Macro

13 Jun 2021, 09:27

Good question. The SetKeyDelay has two parameters (see documentation). The first ("delay") is the time after key up. The second ("press duration") is the time after key down. As you can see, the current script omits the press duration, but you could add a press duration if you like. The default press duration is none, which I think would also explain the zeroes that you saw. Some games require a non-null press duration, about 20-40 ms in many cases.
msy92
Posts: 4
Joined: 05 Jun 2021, 13:01

Re: Random Space Macro

13 Jun 2021, 11:01

mikeyww wrote:
13 Jun 2021, 09:27
Good question. The SetKeyDelay has two parameters (see documentation). The first ("delay") is the time after key up. The second ("press duration") is the time after key down. As you can see, the current script omits the press duration, but you could add a press duration if you like. The default press duration is none, which I think would also explain the zeroes that you saw. Some games require a non-null press duration, about 20-40 ms in many cases.
When I used Razer Synapse 2 I get 0.00 seconds for key down. However when using Razer Synapse 3 its very mixed, see link: https://imgur.com/a/aGwaekN
I Tried to add the press duration but failed after looking at multiple threads, How do you tackle this?

Many thanks!
User avatar
mikeyww
Posts: 26947
Joined: 09 Sep 2014, 18:38

Re: Random Space Macro

13 Jun 2021, 11:08

You may be seeing different ways to measure. As I showed, you can do this in your script directly, to compute each of the times. You may experience very small extra times due to the script itself, and other programs, running; you can adjust your numbers accordingly. You obviously do not have complete control over your CPU, which is doing many things at the same time. Thus, if you think that you will always be able to set the time to 32 ms instead of 31 ms, etc., you are probably mistaken!

There is not much to this entire process, because it's only a few lines, and you can compute and display all of the times in the script. You can change SetKeyDelay to something like SetKeyDelay, wait, 20 if you wish. If you need a smaller wait, just adjust your Random command.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 85 guests