Change the keystroke output when running the script

Ask gaming related questions (AHK v1.1 and older)
deb0phin
Posts: 3
Joined: 28 Jan 2023, 01:59

Change the keystroke output when running the script

Post by deb0phin » 28 Jan 2023, 06:33

I am a newcomer for AHK and now currently learning how to write AHKv2 script.

My idea is to create a script with a simple GUI for AFK in gaming without being kicked, which will simulate us for holding two different keyboard button for a while alternately (default will be "w" and "s").
I also want to include a function that allow me to change the button that going to be held while running the script through GUI, like by invoking the Gui.Add("Hotkey", "...").

which may be something look like this:

Code: Select all

MyGui := Gui()
MyGui.Add("Text",,"Key 1: ")
MyGui.Add("Text",,"Key 2: ")

MyGui.Add("Hotkey", "vChosenHotkey ym")
MyGui.Add("Hotkey", "vChosenHotkey2")
MyGui.Add("Button", "Default" ,"OK").OnEvent("Click", keychange)
MyGui.Show()

keychange(*)
{
    Saved := MyGui.Submit()
    MsgBox("You have chose " Saved.ChosenHotkey " and " Saved.ChosenHotkey2)
    action()
}

action(*)
{
    loop{
        Send "{Saved.ChosenHotkey down}" 
        Sleep 1000
        Send "{Saved.ChosenHotkey up}" 
        Sleep 3000
        Send "{Saved.ChosenHotkey2 down}" 
        Sleep 1000
        Send "{Saved.ChosenHotkey2 down}" 
        Sleep 3000
    } 
}
#Esc::ExitApp
For example, if I type "m" in the MyGui.Add("Hotkey", ...) GUI object, the script should send the "m" to the game instead of "w" as default.
However, the above code doesn't really work as what I expect...

Can I ask how can I modify my code the perform such features?

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

Re: Change the keystroke output when running the script

Post by mikeyww » 28 Jan 2023, 08:23

Welcome to this AutoHotkey forum!

Code: Select all

; This script allows the user to define hotkeys to perform actions
#Requires AutoHotkey v2.0
check := (*) => btn.Enabled := hk1.Value hk2.Value ? True : False
MyGui := Gui(, 'Hotkeys')
MyGui.SetFont('s10')
MyGui.Add("Text", 'w50',"Key 1:")
MyGui.Add("Text", 'wp', "Key 2:")
hk1 := MyGui.Add("Hotkey", "ym")
hk2 := MyGui.Add("Hotkey")
hk1.OnEvent('Change', check)
hk2.OnEvent('Change', check)
btn := MyGui.Add("Button", "xm w214 Default Disabled" ,"OK")
btn.OnEvent("Click", keychange)
MyGui.Show()

keychange(*) {
 MyGui.Submit()
 MsgBox "You have chosen " hk1.Value " and " hk2.Value '.', 'Hotkeys', 64
 Try Hotkey hk1.Value, action, 'On'
}

action(*) {
 SendText 123
}
Explained: Hotkey

Post Reply

Return to “Gaming Help (v1)”