Fxssil
Fxssil

Reputation: 21

AHK Script to Double-Click every X number of Left Clicks (Randomly Double-Click Every Couple Clicks)

I was wondering if there was a way to write a AHK Script to Double-Click the left mouse button every random number of Left Clicks (Randomly Double-Click Every Couple Clicks)

Upvotes: 1

Views: 986

Answers (1)

Spyre
Spyre

Reputation: 2344

Absolutely

From the docs, we have the Random function that

Generates a pseudo-random number.

Using the syntax

Random, OutputVar , Min, Max

Where OutputVar is the output variable, Min is the minimum value (inclusive), and Man is the manimum value (inclusive).

With this information, let's make a subroutine called generateNewRandom that we can call every time every time we want to regenerate our random number.

For this example, I will set the Min to 1 and the Max to 10 so that a random number between [1, 10] inclusive is generated. You can change these values as needed. We will save the output of the Random function into a variable named NumClicks.


Next, let's create a hotkey that will trigger every time the Left Mouse Button is clicked.

However, this hotkey will require several various modifiers (namely: *, ~, and $) in order to function properly. You can find out more about them in the docs.

In short:

  • Wildcard (*) activates the hotkey even if other modifier keys (such as Shift or Ctrl are held down
  • Tilde (~) allows the hotkey's native function to still happen when the hotkey is triggered.
  • Dollar Sign ($) allows a hotkey to Send itself without causing an infinite loop.

For the hotkey itself, when LButton (The Left Mouse Button) is pressed, do the following:

  • decrement numClicks
  • if numClicks is 0
    • Click again
    • Generate a new Random number

In addition to the Subroutine and the Hotkey, we will need to call the subroutine once in the auto-execute section at the beginning in order to generate an initial random value.


Final Script:

gosub generateNewRandom ;Generate initial random number

*~$LButton::
numClicks--
if(numClicks==0){
    Click
    gosub generateNewRandom ;Generate subsequent random numbers
}
return


generateNewRandom:
Random, numClicks , 1, 10 ;Adjust these values as needed
return

Upvotes: 1

Related Questions