Switching between same key macro's Topic is solved

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

Switching between same key macro's

18 Aug 2018, 11:52

Hello,

I currently have a multitude of different files that have different macro's for the same key (namely: g or b) with different behaviour all for the same game.

For example:
file1.ahk

Code: Select all

g::	
While GetKeyState("g"){
Send abcd
Sleep 2
}
return

b::
While GetKeyState("b"){
Send efgh
Sleep 2
}
return
file2.ahk

Code: Select all

g::	
While GetKeyState("g"){
Send azet
Sleep 2
}
return

b::
While GetKeyState("b"){
Send yuo
Sleep 2
}
return
Now my idea was to create a GUI where I could easily switch between these different macro's via radio button or something but I have no idea how to start. If this would be possible I would only have 1 file with all my macro's instead of 10 seperate ones that need me to manualy switch between them.

Any help to get me along the way would be greatly appreciated! (even a refference to a topic where something similar is being done would work, I didn't find any at first glance)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Switching between same key macro's

22 Aug 2018, 12:22

Code: Select all

#NoEnv
#SingleInstance Force
; SendMode Input
; SetBatchLines -1

Gui Switcher: New, +AlwaysOnTop +HwndhSwitcher, Keymap Switcher
Gui Add, Text, xm ym, Keymap #:
Gui Add, Text, ym +HwndhKeymapLabel
Gui Add, Radio, xm gRadioEventHandler vRadioSelection Checked Group, 1
Gui Add, Radio, gRadioEventHandler, 2
Gui Add, Radio, gRadioEventHandler, 3
Gui Add, Radio, gRadioEventHandler, 4
Gui Add, Radio, gRadioEventHandler, 5
Gui Show, xCenter yCenter w300 h150 NoActivate
Gosub RadioEventHandler
Return

Esc::
SwitcherGuiClose:
SwitcherGuiEscape:
	ExitApp
Return

RadioEventHandler:
	Gui Switcher: Submit, NoHide
	swapKeymap(RadioSelection)
	GuiControl Text, % hKeymapLabel, % RadioSelection
Return

swapKeymap(i) {
	static Keymap := {"b": ["efgh", "yuo", "1_three", "1_four", "1_five"]
					, "g": ["abcd", "azet", "2_three", "2_four", "2_five"]}

	for key, SendMsg in Keymap
	{
		fn := Func("customSend").Bind(SendMsg[i])
		Hotkey % key, % fn
	}
}

customSend(msg) {
	key := A_ThisHotkey
	while(GetKeyState(key))
	{
		Send % msg
		Sleep 2
	}
}
TheMythManLegend

Re: Switching between same key macro's

22 Aug 2018, 14:40

Thanks so much for your reply!

However, if I'm not mistaken this will only work as long as the macro is in the same manner:

send somekeys
sleep 2
etc..

But if I were to introduce some macro's with more commands, for example:

Code: Select all

g::
While GetKeyState("g")
{
send {f down}
sleep 265
send {f up}
sleep 1
send r
sleep 25
}
return
and

Code: Select all

g::
	While GetKeyState("g")
	{
	Send r
	Sleep 400
	Send t
	Sleep 220
	Send f
	Sleep 310
	}
return

b::
	While GetKeyState("b")
	{
	Send t
	Sleep 360
	Send r
	Sleep 220
	Send f
	Sleep 310
	}
return
With these macro's I wouldn't be able to integrate them atm. I should have introduced this in my first post already, my bad, eventhough most of them are simple like the first examples :)

Thank you very much for your time already cause with this I will be able to integrate the better part of my macro's into 1 already. I really appreciate it! In the meantime I'll read up and try to get smarter and maybe figure it out myself ;)
TheMythManLegend
Posts: 1
Joined: 22 Aug 2018, 14:42

Re: Switching between same key macro's  Topic is solved

27 Aug 2018, 07:43

I got it to work the way I want it but I'm not sure if the script is very efficient.

Code: Select all

#NoEnv
#SingleInstance Force
; SendMode Input
; SetBatchLines -1

Gui Switcher: New, +AlwaysOnTop +HwndhSwitcher, Keymap Switcher
Gui Add, Text, xm ym, Keymap #:
Gui Add, Text, ym +HwndhKeymapLabel
Gui Add, Radio, xm gRadioEventHandler vRadioSelection Checked Group, 1
Gui Add, Radio, gRadioEventHandler, 2
Gui Add, Radio, gRadioEventHandler, 3
Gui Add, Radio, gRadioEventHandler, 4
Gui Add, Radio, gRadioEventHandler, 5
Gui Show, xCenter yCenter w300 h150 NoActivate
Gosub RadioEventHandler
Return

Esc::
SwitcherGuiClose:
SwitcherGuiEscape:
	ExitApp
Return

RadioEventHandler:
	Gui Switcher: Submit, NoHide
	swapKeymap(RadioSelection)
	GuiControl Text, % hKeymapLabel, % RadioSelection
Return

swapKeymap(i) {
	static Keymap := {"b": [Func("function1"), "yuo", "1_three", "1_four", "1_five"]
					, "g": ["", "azet", "2_three", "2_four", "2_five"]}

	for key, SendMsg in Keymap
	{
		if SendMsg[i] = & !IsFunc(SendMsg[i])
		{
			Hotkey % key, Off, UseErrorLevel
			if ErrorLevel in 5
				return
			return
		}
		else
		{
			fn := Func("customSend").Bind(SendMsg[i])
			Hotkey % key, % fn
		}
	}
}

customSend(msg) {
	key := A_ThisHotkey
	while(GetKeyState(key))
	{
		if IsFunc(msg) {
			msg.Call()
		} else {
			Send % msg
			Sleep 2
		}
	}
}

function1()
{
	Send r
	Sleep 400
	Send t
	Sleep 220
	Send f
	Sleep 310
}
I can now add the few multi-line macro's I have as functions while keeping the rest as they are. Also in addition I can opt for an empty string "" if I don't want to make a macro for a certain key. (first radiobutton g-key for example, will work as a normal "g" instead of a hotkey).

This now works as I want it to work. If there's any improvements I can make, let me know. Special thanks to swagfag for guiding me towards my solution.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: Rohwedder and 67 guests