Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Merging two scripts



  • Please log in to reply
2 replies to this topic
OlleDenStore
  • Members
  • 6 posts
  • Last active: Sep 20 2015 05:08 PM
  • Joined: 21 Apr 2015

Hi!

 

I'm new to autohotkey and I'm trying to write my first script. What I want to do is this:

When left mouse button is pressed at the same time as the space bar I want to send "2" once every second and "4" once every 15 seconds.

 

Right now I'm doing this with two different scripts both running at the same time. They are as follows:

1.

Loop
{
If (GetKeyState("Space", "P") && GetKeyState("LButton", "P"))
	Send 2
	sleep 1000
}

2.

Loop
{
If (GetKeyState("Space", "P") && GetKeyState("LButton", "P"))
	Send 4
	sleep 15000
}

This works but I would like to merge them into one script and also I don't like the busy wait loop. There has to be a better way to do this.

Any and all help would be greatly appreciated.

 

/Olle



egocarib
  • Members
  • 42 posts
  • Last active: Aug 12 2017 10:04 PM
  • Joined: 17 Apr 2015

The easiest way would be to simply use a modulus operation to determine if the loop number is divisible by 15:

Loop
{
    If (GetKeyState("Space", "P") && GetKeyState("LButton", "P"))
    {
	Send 2
        If (Mod(A_Index, 15) = 0)
        {
            Send 4
        }
    }
    sleep 1000
}

Another option is to use SetTimer instead of the loops. You can set up mutiple timers.

 

Example:

#Persistent
SetTimer, TimerTwo, 1000
SetTimer, TimerFour, 15000
return

TimerTwo:
    If (GetKeyState("Space", "P") && GetKeyState("LButton", "P"))
        Send 2
return

TimerFour:
    If (GetKeyState("Space", "P") && GetKeyState("LButton", "P"))
        Send 4
return


OlleDenStore
  • Members
  • 6 posts
  • Last active: Sep 20 2015 05:08 PM
  • Joined: 21 Apr 2015
✓  Best Answer

Thanks for your quick reply!

 

I ended up using the second example you wrote. It works great and I even managed to add a hotkey to toggle the script on and off by myself :)

Now it looks like this:

#Persistent
SetTimer, TimerTwo, 800
SetTimer, TimerFour, 14700

return

f1::

Pause

Suspend

return

TimerTwo:
    	If (GetKeyState("Space", "P") && GetKeyState("LButton", "P"))
        	Send 2
return

TimerFour:
    	If (GetKeyState("Space", "P") && GetKeyState("LButton", "P"))
        	Send 4
return

The f1 key now toggles it on and off. How would I go about assigning two hotkeys to toggle? One to toggle TimerTwo and one to toggle TimerFour.

 

Edit: Nevermind, I figured it out :)

 

/Olle