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

Ask gaming related questions

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


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

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

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
Posts: 146
Joined: 01 Oct 2013, 09:47
GitHub: MasterFocus
Location: Rio de Janeiro - RJ - Brasil

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


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


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


    win_title := "Untitled - Notepad"
    IfWinNotExist, %win_title%
    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% )
            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)
Antonio França - | | |
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”

Who is online

Users browsing this forum: No registered users and 13 guests