Making a auto hotkey script that is specific to one key work for a variable keys

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Dkgamga
Posts: 2
Joined: 24 Nov 2017, 08:33

Making a auto hotkey script that is specific to one key work for a variable keys

24 Nov 2017, 09:06

Hello,
I am new to AHK and trying to create a program which allows auto clicking and other functions by detecting if a key is held down.

I have made the code work for a specific key (Extra mouse button 'XButton2') but am having trouble making it work for a variable key.

I need the code to use a function like bellow to determine the state of a key (EG: XButton2) which then will allow it to perform a defined task through another function (EG: RepeatSpamClick)

Code: Select all

*$~XButton2::
	Checkstate(XButton2,RepeatSpamClick)
return

Code: Select all

RepeatSpamClick:
	MouseClick, left,,, 1, 0, D
	Sleep, 10
	MouseClick, left,,, 1, 0, U
	Sleep 100
return
This is a snippet of the working code which detects when XButton2 is pressed and then runs a function to rapidly right click:

Code: Select all

*$~XButton2::
	GetKeyState, XButton2State, XButton2
	if XButton2State = D
	Loop
	{	
		GetKeyState, XButton2State, XButton2
			if XButton2State = U
				break	
			RepeatRapidSpamL(XButton2_SpamL_Delay)
	}
RepeatRapidSpamL(XButton2_SpamL_Delay)
{
	MouseClick, left,,, 1, 0, D
	Sleep, 10
	MouseClick, left,,, 1, 0, U
	Sleep %XButton2_SpamL_Delay%
Here is the full working code so far if the program is unclear:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance,Force

Gui, +AlwaysOnTop
Gui, Color,		Black
Gui, Font,	 	cWhite
Hotkey,*$~XButton2,off
Hotkey,*$~XButton1,off

;-------------------------------------------
Gui, Add, 		Text, , Which Hotkeys would you like to activate?`n

Gui, Add,	 	Checkbox, vXButton1_SpamR_State x10 group gSubmit_XButton1_SpamR_Check, XButton1 Rapid Clicker

Gui, Add, 		Edit, vXButton1_SpamR_Delay cBlack x27 limit4 number Disabled  gSubmit,Delay(ms) between clicks

Gui, Add,	 	Checkbox, vXButton2_SpamL_State x10 group gSubmit_XButton2_SpamL_Check, XButton2 Rapid Clicker

Gui, Add, 		Edit, vXButton2_SpamL_Delay cBlack x27 limit4 number Disabled  gSubmit,Delay(ms) between clicks



;~ Gui, Add,

Gui, Show, 		w225 h200 Center, Active Hotkeys  
Gui, Submit, 	NoHide
return
;-------------------------------------------
Submit:
		gui, 	Submit, NoHide
	return
;Spam Left Click Active-------------------------------------------
Submit_XButton2_SpamL_Check:
		gui, 	Submit, NoHide
		If (XButton2_SpamL_State=1)
		{
			GuiControl,Enabled,XButton2_SpamL_Delay
			Hotkey,*$~XButton2,on
		}
		Else
		{
			GuiControl,Disabled,XButton2_SpamL_Delay
			Hotkey,*$~XButton2,off
		}

	return
;Spam Left Click Hotkey--------------------------------------
*$~XButton2::
	GetKeyState, XButton2State, XButton2
	if XButton2State = D
	Loop
	{	
		GetKeyState, XButton2State, XButton2
			if XButton2State = U
				break	
			RepeatRapidSpamL(XButton2_SpamL_Delay)
	}
RepeatRapidSpamL(XButton2_SpamL_Delay)
{
	MouseClick, left,,, 1, 0, D
	Sleep, 10
	MouseClick, left,,, 1, 0, U
	Sleep %XButton2_SpamL_Delay%
}
;Spam Right Click Active-------------------------------------------
Submit_XButton1_SpamR_Check:
		gui, 	Submit, NoHide
		If (XButton1_SpamR_State=1)
		{
			GuiControl,Enabled,XButton1_SpamR_Delay
			Hotkey,*$~XButton1,on
		}
		Else
		{
			GuiControl,Disabled,XButton1_SpamR_Delay
			Hotkey,*$~XButton1,off
		}

	return
;SpamR Right Click Hotkey--------------------------------------
*$~XButton1::
	GetKeyState, XButton1State, XButton1
	if XButton1State = D
	Loop
	{	
		GetKeyState, XButton1State, XButton1
			if XButton1State = U
				break	
			RepeatRapidSpamR(XButton1_SpamR_Delay)
	}
RepeatRapidSpamR(XButton1_SpamR_Delay)
{
	MouseClick, right,,, 1, 0, D
	Sleep, 10
	MouseClick, right,,, 1, 0, U
	Sleep %XButton1_SpamR_Delay%
}


;-------------------------------------------
return
^!+x::
	ExitApp

I have made serval attempts encountering many issues such as:
This code was an attempt at detecting the state of a variable key activate by another key which outputted the correct values (1 when pressed and 0 when unpressed)

Code: Select all

Key := "XButton2"

Numpad0::
	%Key%State := GetKeyState(Key,"p")
	Msgbox, %XButton2State%
	return
However when changing the activation key to the checked key the output is the incorrect value (0-Unpress) despite me pressing the key down while triggering the code

Code: Select all

Key := "XButton2"
*$~XButton2::
	%Key%State := GetKeyState(Key,"p")
	Msgbox, %XButton2State%
	return
Last edited by Dkgamga on 25 Nov 2017, 04:56, edited 1 time in total.
Nightwolf85
Posts: 302
Joined: 05 Feb 2017, 00:03

Re: Making a auto hotkey script that is specific to one key work for a variable keys

24 Nov 2017, 10:38

I'm not sure what you are trying to accomplish, the GetKeyState function you use in your attempts at the bottom already does what you are asking for it seems ( A function to determine the state of a key ). And using your exact example code has the expected results for me.

Are you trying to make the activator key not the same as the key you want held?

I've made some modifications, that I doubt accomplish what you want, but it does show off how to use the same code to handle multiple hotkeys, which may be relevant.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance,Force

Gui, +AlwaysOnTop
Gui, Color,		Black
Gui, Font,	 	cWhite
Hotkey,*$XButton2,off
Hotkey,*$XButton1,off

;-------------------------------------------
Gui, Add, 		Text, , Which Hotkeys would you like to activate?`n

Gui, Add,	 	Checkbox, vXButton1_Spam_State x10 group gSubmit_XButton1_SpamR_Check, XButton1 Rapid Clicker

Gui, Add, 		Edit, vXButton1_Spam_Delay cBlack x27 limit4 number Disabled  gSubmit,Delay(ms) between clicks

Gui, Add,	 	Checkbox, vXButton2_Spam_State x10 group gSubmit_XButton2_SpamL_Check, XButton2 Rapid Clicker

Gui, Add, 		Edit, vXButton2_Spam_Delay cBlack x27 limit4 number Disabled  gSubmit,Delay(ms) between clicks



;~ Gui, Add,

Gui, Show, 		w225 h200 Center, Active Hotkeys  
Gui, Submit, 	NoHide
return
;-------------------------------------------
Submit:
		gui, 	Submit, NoHide
	return
	
;Spam Left Click Active-------------------------------------------
Submit_XButton2_SpamL_Check:
		gui, 	Submit, NoHide
		If (XButton2_Spam_State=1)
		{
			GuiControl,Enabled,XButton2_Spam_Delay
			Hotkey,*$XButton2,on
		}
		Else
		{
			GuiControl,Disabled,XButton2_Spam_Delay
			Hotkey,*$XButton2,off
		}

	return
	
;Spam Right Click Active-------------------------------------------
Submit_XButton1_SpamR_Check:
		gui, 	Submit, NoHide
		If (XButton1_Spam_State=1)
		{
			GuiControl,Enabled,XButton1_Spam_Delay
			Hotkey,*$XButton1,on
		}
		Else
		{
			GuiControl,Disabled,XButton1_Spam_Delay
			Hotkey,*$XButton1,off
		}

	return

;Spam Click Hotkey--------------------------------------
*$XButton2::
*$XButton1::
	useKey := RegExReplace(A_ThisHotkey, "[*$]") ; Remove anything that isn't the key name
	useDelay := %useKey%_Spam_Delay ; I changed your variable names above to make this work.
	While (GetKeyState(useKey,"P"))
		RepeatRapidSpam(useKey,useDelay)
Return

RepeatRapidSpam(key, Spam_Delay)
{
	IF (key = "XButton1")
		useClick := "right"
	Else IF (key = "XButton2")
		useClick := "left"
	Else
		Return
	MouseClick, %useClick%,,, 1, 0, D
	Sleep, 10
	MouseClick, %useClick%,,, 1, 0, U
	Sleep %Spam_Delay%
}

;-------------------------------------------
return
^!+x::
	ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], jameswrightesq, wpulford and 421 guests