RapidFire Script for AHKv2, Left Mouse Button with Toggle On/Off Topic is solved

Ask gaming related questions
Tobgun1
Posts: 123
Joined: 23 Feb 2023, 07:28

RapidFire Script for AHKv2, Left Mouse Button with Toggle On/Off

Post by Tobgun1 » 25 Mar 2023, 19:54

Helo :) i try to integrate a Left Mouse RapidFire function with On/Off Toggle into my Script

i get errors with the Parameter #2 on the command, my main problem is there are of cuse 2000 Topics for RapidFire, but all are for V1, and to adapt it into V2 seems more complicated then i thought
Note: i Tried severall combinations to get the misstake, i tried ("LButton","P":=1) , P0 , ("LButton","D"), ("LButton",'P') and ("LButton","P":=1) and !GetKeyState("LButton",'P')

her are my Script Parts

this is the On/Off Toggle:

Code: Select all

RapidFireFunction(*) {
    Static on := False
    if on := !on {
     ComObject("SAPI.SpVoice").Speak("RapidFire Aktiviert")  
     SetTimer(RapidFire, 1)
    } Else {
     ComObject("SAPI.SpVoice").Speak("RapidFire Deaktiviert")
     SetTimer(RapidFire, 0)
    }
}

and RapidFire Left Mouse Button, when Pushed Down, Stop when Release Button, how i interpreted it:

Code: Select all

RapidFire() {
    while GetKeyState("LButton","P":=1)
    Loop {
    SetMouseDelay(20)
    Click
    if GetKeyState("LButton","P":=0)
        break
    }
}
i tryed to understand the docs of
https://www.autohotkey.com/docs/v2/lib/GetKeyState.htm

Code: Select all

        if !GetKeyState("NumpadAdd", "P")  ; The key has been released, so break out of the loop.
            break
so i dont understand why ("LButton", "P") gives a error

Edit: i forgot:
with:

Code: Select all

RapidFire() {
    while GetKeyState("LButton","P")
    Loop {
    SetMouseDelay(20)
    Click
    if GetKeyState("LButton","P")
        break
    }
}
i can toggle an instant ongoing speed click , and nearly cant toggle Off it with my hotkey because the click speed is faster then the Hotkey send
how can i set the Loop to "off when release mouse button" ?

thanks for any help or lead into a direction :)

Tobgun1
Last edited by Tobgun1 on 27 Mar 2023, 04:57, edited 1 time in total.

Tobgun1
Posts: 123
Joined: 23 Feb 2023, 07:28

Re: Get keystate Problem with RapidFire beacuse of AHKv2, Toggle On off and more

Post by Tobgun1 » 26 Mar 2023, 09:27

@mikeyww
Sorry to bother you, i got this complied togheter with help of my Tudor, it is mainly your work

Code: Select all

RapidFireOn := False
RapidFireFunction(*) {
        Global RapidFireOn := !RapidFireOn
        SoundBeep 1000 + 500 * RapidFireOn 
       }
       #HotIf RapidFireOn
       ~$LButton:: {
           While GetKeyState("LButton", "P") {
           Click
           Sleep(25)
           }
       }
       #HotIf
i do not understand why i cant change "SoundBeep" into

Code: Select all

ComObject("SAPI.SpVoice").Speak("On") + ComObject("SAPI.SpVoice").Speak("Off") * RapidFireOn
could u maybe explain it to me or help me out?
i tried now severall "if" combinations too

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

Re: Get keystate Problem with RapidFire beacuse of AHKv2, Toggle On off and more  Topic is solved

Post by mikeyww » 26 Mar 2023, 10:22

Hello,

To understand this, you can run the following script.

Code: Select all

#Requires AutoHotkey v2.0
MsgBox ComObject("SAPI.SpVoice").Speak("Off")
The script above demonstrates how you can use the script itself to understand the code.

Setting a timer that executes a loop, or executing a loop inside a While statement, is often a malformed plan. There may be situations where it is needed. To understand the script, follow it line by line.

Code: Select all

#Requires AutoHotkey v2.0
rapid := False

F3:: {
 Global rapid := !rapid
 ComObject('SAPI.SpVoice').Speak('RapidFire ' (rapid ? 'Aktiviert' : 'Deaktiviert'))
}

#HotIf rapid
LButton:: {
 SetKeyDelay 25, 25
 While GetKeyState(ThisHotkey, "P")
  SendEvent '{' ThisHotkey '}'
}
#HotIf
Concepts of this script:
1. A hotkey toggles a flag that defines a context for a different hotkey.
2. A single loop is used to create an auto-clicker.
3. Global is used to assign a global variable inside a function in a v2 script.
4. A global variable is defined at the top of the script.
5. SetKeyDelay is applied to the SendEvent function.
6. ThisHotkey always refers to the hotkey that triggered this hotkey subroutine.

Tobgun1
Posts: 123
Joined: 23 Feb 2023, 07:28

Re: Get keystate Problem with RapidFire beacuse of AHKv2, Toggle On off and more

Post by Tobgun1 » 26 Mar 2023, 19:17

mikeyww wrote:
26 Mar 2023, 10:22
Hello,

To understand this, you can run the following script.

Code: Select all

#Requires AutoHotkey v2.0
MsgBox ComObject("SAPI.SpVoice").Speak("Off")
The script above demonstrates how you can use the script itself to understand the code.

Setting a timer that executes a loop, or executing a loop inside a While statement, is often a malformed plan. There may be situations where it is needed. To understand the script, follow it line by line.

Code: Select all

#Requires AutoHotkey v2.0
rapid := False

F3:: {
 Global rapid := !rapid
 ComObject('SAPI.SpVoice').Speak('RapidFire ' (rapid ? 'Aktiviert' : 'Deaktiviert'))
}

#HotIf rapid
LButton:: {
 SetKeyDelay 25, 25
 While GetKeyState(ThisHotkey, "P")
  SendEvent '{' ThisHotkey '}'
}
#HotIf
Concepts of this script:
1. A hotkey toggles a flag that defines a context for a different hotkey.
2. A single loop is used to create an auto-clicker.
3. Global is used to assign a global variable inside a function in a v2 script.
4. A global variable is defined at the top of the script.
5. SetKeyDelay is applied to the SendEvent function.
6. ThisHotkey always refers to the hotkey that triggered this hotkey subroutine.
INCREDIBLE, Amazing and Incredi-mazing :D

it is Insane what you are able to Build up!
Thank you very very much for taking your Time for it, and for the Explanations.

it is Crazy how a skilled User like u can Jump above severall commands, Like Click Left Down , short it to Click L D and short it to Click D.

i do not understand ComObject('SAPI.SpVoice').Speak('RapidFire ' (rapid ? 'Aktiviert' : 'Deaktiviert'))
how this " Toggle between both sounds" work, the left part ask "?" is Rapid active? then it uses the left side, if not ":" it uses the right side
where or how do i find such a combination / information?

i think i understood 1 and 2
3 - i do not now understand the difference why it needs to be a global variable, and not a "toggle on off" (if that is the right translation again :( )
4 - okay good to know
5 - so my SetKeyDelay or Set MouseDelay didnt work because the Click is what? , if u change it to a Send Event why does SetKeyDelay apply then? while it is still Left Mouse Button? because it is a "key" throught "Hotkey" function?
and not a mouse button?

6 - good to know, but then the Used hotkey gets recognized as a "Key" from then on? , so Mouse Left button is not a SetMouseDelay it is a SetKeyDelay?


does the SetKeyDelay inside the loop still work even if i done SetKeyDelay(800, 100)
on top of my regular script?
because it is in its own 'HotIf Section ?

i wonder even funny is i got stuff like:
SetKeyDelay(800, 100)
SendMode("Event")

u wrote it
SetKeyDelay 25, 25
SendEvent '{' ThisHotkey '}'

really crazy with all those differences in meaning,
i appreciate ur answer and your solution :clap: :thumbup:

if u want to u can go on answering my questions but for now i understand i am not the only one here asking for your skilled help :D
Last edited by Tobgun1 on 26 Mar 2023, 19:50, edited 3 times in total.

Tobgun1
Posts: 123
Joined: 23 Feb 2023, 07:28

Re: Get keystate Problem with RapidFire beacuse of AHKv2, Toggle On off and more

Post by Tobgun1 » 26 Mar 2023, 19:33

@mikeyww

btw, it would be great if you might post this one online with topic "RapidFire Script for v2" i was searching a lot and nothing was a solution like this
and mainly searching users like me, still land in v1 Threaths with ... uhm... let me say "missleading" informations

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

Re: Get keystate Problem with RapidFire beacuse of AHKv2, Toggle On off and more

Post by mikeyww » 26 Mar 2023, 20:10

You have good questions, although the documentation already answers them.

I do occasionally initiate posts, but usually answer them instead. The initiator defines the subject of the post. You can use any text that you like (preferably related to your specific topic, question, or problem!).

For the ?: syntax, see "ternary operator" in the documentation. The ternary operator has three parts: If... then... else.

To assign a global variable inside a function, a Global declaration in the function is needed. That is how AutoHotkey v2 works.

You can use SetMouseDelay with Click if you wish. The delay occurs after the click (apparently). The truth is that I've never used it successfully, and there are some other notes on the forum indicating that the delay actually happens before the button is released. I am not the authority, but that is what it looks like when I test it. In any case, with SetKeyDelay, you can define both a delay (after keystroke) and a press duration. This is sometimes useful. By default, SetKeyDelay applies to SendEvent (and never SendInput). Your original use of Click may be fine, too. I sometimes just show alternatives.

ThisHotkey is a special variable that refers to the hotkey that triggered the enclosing subroutine. You do not need to use it.

If SetKeyDelay occurs both at the top and inside the subroutine or thread, the one inside the subroutine takes precedence. This is explained in the documentation.
Every newly launched thread (such as a hotkey, custom menu item, or timed subroutine) starts off fresh with the default setting for this function. That default may be changed by using this function during script startup.
Yes, you can change the SendMode instead of using SendEvent (for example).

As you can see, there are sometimes multiple ways to accomplish the same thing.

Many of these questions can be answered simply by running a test script. This can be helpful because it also enables you to see the effects yourself, instead of just reading about them.

Tobgun1
Posts: 123
Joined: 23 Feb 2023, 07:28

RapidFire Script for AHKv2, Left Mouse Button with Toggle On/Off

Post by Tobgun1 » 27 Mar 2023, 04:56

Wow what a great response 😀👍🙏

Really thanks a lot! I know it looks many times like I do not read the docs, but truly I have several times problems to understand them, sometimes it seems to be a translation sided problem. I even feel pretty dumb sometimes.
I tried several test scripts out of em and I am not sure what they r supposed to tell.
Maybe it is some kind of problem I have in understanding languages? 🤷🏻‍♂️🙈 I wonder why becuse in my area I am pretty good in english. But maybe we germans just think we understand english 😂🙈


Example: I read the documentation about using () in parameters is not needed.

But still me and even my tutor uses em 90% of the time.

We are using Visual Studio Code as Script Editor.
It looks very cool and gives me seversll times a ) when I start with a (
Or a second " when I start with one.

It seems this editor relates me to use () when I write SetKeyDelay and a number . If I didnt done it (after your example i re tried it) it still accepted it 😂🙈🤷🏻‍♂️

That's how I can explain something like that for now.

And yes the way to write something in it's own style is, in my opinion, wayyyy to many possible differences in AHK

It is like base language besides professional coder language.

So most of the time it seems like every answer to a beginner question, gets answered with some kind of "Answerers own style"

And yeah right now I struggle with the fact that the internet is filled with AHKv1 posts and explanations.

Thanks for the lead to the "ternary operator " I gonna read that after work 😀👍

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

Re: RapidFire Script for AHKv2, Left Mouse Button with Toggle On/Off

Post by mikeyww » 27 Mar 2023, 08:21

You are right: there are different styles, not only for the syntax, but for how whitespace is used, where braces are placed, and so on. My writing style is not necessarily popular. I often put related function calls on one line-- some do not like it-- and put the braces in less common positions (after a function call instead of on a separate line). A forum reader simply has to deal with what they see. I am always glad to answer any questions about meaning. I appreciate AHK's flexibility in syntax, as it can save coding time and sometimes aid with readability. For example, without going to extreme and complicated measures, parentheses for functions would have to be required when a line contains more than one function call, but having the ability to write multiple lines of individual function calls without parentheses saves coding time, as it requires fewer key presses. It also reads well. That is just my opinion, and I know that some disagree. For people who used v1, this format (syntax, or layout) is also familiar.

Auf wiedersehen.

Tobgun1
Posts: 123
Joined: 23 Feb 2023, 07:28

Re: RapidFire Script for AHKv2, Left Mouse Button with Toggle On/Off

Post by Tobgun1 » 27 Mar 2023, 09:34

Yeah 👍 good point and another nice reply!

Auf wiedersehen
😅
You are great! I think this forum needs you and would be half death without you! 👍💪🔥

So as Schwarzenegger once said "I'll be back" 😂🙈

Thanks for your time @mikeyww

Katagua
Posts: 1
Joined: 15 Sep 2023, 13:50

Re: RapidFire Script for AHKv2, Left Mouse Button with Toggle On/Off

Post by Katagua » 15 Sep 2023, 13:54

Hello,

Ive tried using this script but I cant manage to set an amount of clicks per second. Ive tried changing the parameters of SetKeyDelay to 83,83 to get around 12 cps but still get over 31. Can someone help me on this ?

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

Re: RapidFire Script for AHKv2, Left Mouse Button with Toggle On/Off

Post by mikeyww » 15 Sep 2023, 15:50

You might want to look at a different kind of approach.

viewtopic.php?p=460435#p460435

Noitalommi_2
Posts: 194
Joined: 16 Aug 2023, 10:58

Re: RapidFire Script for AHKv2, Left Mouse Button with Toggle On/Off

Post by Noitalommi_2 » 16 Sep 2023, 02:21

@Katagua
Hi, here's a V2 script, take a look.

viewtopic.php?f=95&t=115469#p534668

Post Reply

Return to “Gaming”