Defining output based on Hotkey? or a workaround Topic is solved

Ask gaming related questions (AHK v1.1 and older)
faviono

Defining output based on Hotkey? or a workaround

13 Nov 2015, 02:51

Last night on IRC, Neuromancer fixed some code for me and ended up with:

toggle := false

return

$1::
SetTimer, spam, % (toggle := !toggle) ? 50 : "off"
return

spam:
ifWinActive, Untitled - Notepad
{
winGet, npid, list, Untitled - Notepad
Loop % npid
{
id=npid%A_Index%
controlSend,, {Blind}{1}, % "ahk_id " %id%
Sleep (300)
}
}
return

This allows "1" to toggle on/off the spam of "1" in the main and background windows. I would like to take this and make multiple key assignments (if 2 is pressed, spam 2 in both windows until toggled off, 3 spams 3, same with shift modifiers, etc.). I was planning on just adding additional hot keys and spam labels. (e.g. $2:: SetTimer, spam2... etc). This is how I've done it in the past, but some of my scripts have become lengthy... Ideally, I was hoping to have some sort of global declaration of characters and if any character (1) or modifier (shift+1) was pushed, it would result in the same toggle functionality.

Is there a better way to accomplish this? Maybe I'll dream of a solution tonight.
User avatar
MasterFocus
Posts: 146
Joined: 01 Oct 2013, 09:47
Location: Rio de Janeiro - RJ - Brasil
Contact:

Re: Defining output based on Hotkey? or a workaround  Topic is solved

14 Nov 2015, 00:01

Use the Code tag when posting code. ;)
The following code is untested, but perhaps you want something like this to spam numbers from 1 to 9:

Code: Select all

Loop, 9 ; <---- note: value "9" hardcoded here
{
    toggle_%A_Index% := false
    Hotkey, % "$" A_Index, toggle_label, On
}
SetTimer, spam_label, -50 ; 50ms is almost like starting right away
Return

;-----------------------------

; Press ESCAPE to exit the script (always a good advice to have a safety measure like this)
*ESC:: EXITAPP

;-----------------------------

toggle_label:
    key := SubStr(A_ThisHotkey,0)
    toggle_%key% := !(toggle_%key%)
Return

;-----------------------------

spam_label:
    win_title := "Untitled - Notepad"
    IfWinNotExist, %win_title%
        Return
    WinGet, pid_list, list, %win_title%
    Loop, %pid_list%
    {
        current_id := A_Index
        Loop, 9 ; <---- note: value "9" hardcoded here
        {
            current_key := A_Index
            If !( toggle_%current_key% )
                Continue
            ControlSend,, {Blind}%current_key%, ahk_id %current_id%
            ;;Sleep, 20 ; <--- commented: delay between keys, if desired
        }
        Sleep, 300 ; <--- delay between spamming each window
    }
    SetTimer, %A_ThisLabel%, -50 ; <--- changing this will affect the delay between each ENTIRE spam cycle (the actual delay here is 50ms)
Return
Antonio França - git.io | github.com | ahk4.net | sites.google.com
Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.
Need help? Please post on the forum before sending me a PM.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 65 guests