I need help improving an AHK.

Ask gaming related questions (AHK v1.1 and older)
kjaty
Posts: 36
Joined: 10 Dec 2019, 22:02

I need help improving an AHK.

11 Dec 2019, 09:34

I have a no backsliding script, but I would like it to only turn the mouse down when I'm shooting, and in my case regardless of whether I'm in the settings menu or out of the game the script is active.
Can anybody help me?

Code: Select all

#NoEnv
SendMode Input

~G::Suspend
~2::Suspend
~1::Suspend



~LButton::
Loop
{

GetKeyState, state, Lbutton, P
if state=u
break

mouseXY(0,7)
sleep 1
mouseXY(0,4)
sleep 1
mouseXY(0,-1)
sleep 5
mouseXY(-1,0) ;
sleep 5
mouseXY(1,0) ;
sleep 5

}
return

mouseXY(x,y)
{
DllCall("mouse_event",int,1,int,x,int,y,uint,0,uint,0)
}
[Mod edit: [code][/code] tags added]
kjaty
Posts: 36
Joined: 10 Dec 2019, 22:02

Re: I need help improving an AHK.

13 Dec 2019, 21:46

I added the command as you said, but the macro does not work. The mouse doesn't come back in the game menus, but when I'm shooting with the gun, it doesn't work either. I wish the macro was not active in the game menus, otherwise.
Sorry for the bad english.
kjaty
Posts: 36
Joined: 10 Dec 2019, 22:02

Re: I need help improving an AHK.

13 Dec 2019, 23:48

PipeDreams wrote:
13 Dec 2019, 23:30
Did you add the name of the game correctly where it says NameOfGameHere?

please post the script with your edit so i can see.

Code: Select all

#NoEnv
SendMode Input
#ifwinactive, PLAYERUNKNOWN'S BATTLEGROUNDS LITE
~G::Suspend
~2::Suspend
~1::Suspend



~LButton::
Loop
{

GetKeyState, state, Lbutton, P
if state=u
break

mouseXY(0,7)
sleep 1
mouseXY(0,4)
sleep 1
mouseXY(0,-1)
sleep 5
mouseXY(-1,0) ;vai para esquerda
sleep 5
mouseXY(1,0) ;vai para esquerda
sleep 5

}
return

mouseXY(x,y)
{
DllCall("mouse_event",int,1,int,x,int,y,uint,0,uint,0)
}
[Mod edit: [code][/code] tags added]
User avatar
PipeDreams
Posts: 165
Joined: 19 Dec 2015, 00:20

Re: I need help improving an AHK.

14 Dec 2019, 03:56

Well, if the script no longer works in game after adding that line, you may need to try a different name. Try using the ahk_exe version of the name.
For example if i wanted to set the script to run only in Destiny2 I could do this:

Code: Select all

#IfWinActive, ahk_exe Destiny2.exe
Instead of:

Code: Select all

#IfWinActive, Destiny2
Use the AHK spy tool to find out how AHK sees the window/program/game and edit accordingly.
Last edited by PipeDreams on 15 Dec 2019, 03:22, edited 1 time in total.
kjaty
Posts: 36
Joined: 10 Dec 2019, 22:02

Re: I need help improving an AHK.

14 Dec 2019, 11:56

PipeDreams wrote:
14 Dec 2019, 03:56
Well, if the script no longer works in game after adding that line, you may need to try a different name. Try using the ahk_exe version of the name.
For example if i wanted to set the script to run only in Destiny2 I could do this:

Code: Select all

#IfWinActive, ahk_exe Destiny2.exe
Instead of:

Code: Select all

#IfWinActive, Destiny2
Use the AHK spy tool to find out how AHK sees the window/program/game and edit accordingly.

Also you can use this to toggle

I was able to do what you told me, now I wanted to do a little better.
Example: Each time you press the number 2 or the letter G to turn off the macro, both of these must turn off the macro, and when you press the number 1, the macro always restarts.
User avatar
PipeDreams
Posts: 165
Joined: 19 Dec 2015, 00:20

Re: I need help improving an AHK.

15 Dec 2019, 03:44

Something like this then?

Code: Select all

~1 Up:: 			;press the number 1, the macro always restarts
Suspend, Permit		;Permits the key to be excluded from suspension.
{	Suspend, OFF 	;Suspend OFF, HotKeys Are Not Suspended.
} Return

~2 Up::				;the number 2 or the letter G to turn off the macro
~g Up::				;the number 2 or the letter G to turn off the macro
{	Suspend, ON 	;Suspend ON, HotKeys Are Suspended.
} Return
or you could just do this.

Code: Select all

~1 Up::Suspend, OFF
~2 Up::Suspend, ON
~g Up::Suspend, ON
kjaty
Posts: 36
Joined: 10 Dec 2019, 22:02

Re: I need help improving an AHK.

15 Dec 2019, 12:07

PipeDreams wrote:
15 Dec 2019, 03:44
Something like this then?

Code: Select all

~1 Up:: 			;press the number 1, the macro always restarts
Suspend, Permit		;Permits the key to be excluded from suspension.
{	Suspend, OFF 	;Suspend OFF, HotKeys Are Not Suspended.
} Return

~2 Up::				;the number 2 or the letter G to turn off the macro
~g Up::				;the number 2 or the letter G to turn off the macro
{	Suspend, ON 	;Suspend ON, HotKeys Are Suspended.
} Return
or you could just do this.

Code: Select all

~1 Up::Suspend, OFF
~2 Up::Suspend, ON
~g Up::Suspend, ON

Friend, just perfect, you must be the AHK GOD.

Abusing a little of your help, I would like to try a few more things.
Example, would it be possible when I press TAB on the keyboard the macro doesn't work and when I press TAB again to exit the menu it reactivates the macro?

Another question?
In this part of the macro below, when I press the key it pulls the mouse down, but it seems that sleep does not work properly because after five seconds it starts to rise even though I still have the button pressed.

Example of the part I told you about.

Code: Select all

~ LButton ::
Loop
{

GetKeyState, state, Lbutton, P
if state = u
break

mouseXY (0.7)
sleep 1
mouseXY (0.4)
sleep 1
mouseXY (0, -1)
sleep 5
mouseXY (-1,0); go left
sleep 5
mouseXY (1,0), go left
sleep 5

}
return
[Mod edit: [code][/code] tags added]
User avatar
PipeDreams
Posts: 165
Joined: 19 Dec 2015, 00:20

Re: I need help improving an AHK.

15 Dec 2019, 15:01

A few things:
I am no AHK god, there are a lot of people that know waaaaaaaaaaaaaaayyyy more than I do about AHK lol, but thanks for the compliment.

This work fine on my end, you just had the HotKey wrong, it should look like this

Code: Select all

~LButton::
not this

Code: Select all

~ LButton ::

Code: Select all

~LButton::
While, GetKeyState("LButton", "P")
{	mouseXY(0,7)
	sleep 1
	mouseXY(0,4)
	sleep 1
	mouseXY(0,-1)
	sleep 5
	mouseXY(-1,0) ; go left 
	sleep 5
	mouseXY(1,0) ; go left 
	sleep 5
	
	mouseXY(x,y)
	{	DllCall("mouse_event",int,1,int,x,int,y,uint,0,uint,0)
	}
} Return

Also, look out for stuff like this:

Code: Select all

mouseXY (-1,0); go left ;<<< You need to have one space between the ) and ; or the script will not work correctly. 
 sleep 5
mouseXY (1,0), go left ;<<< You can only comment with a semicolon, not a comma; the extra comma is causing an issue.

I can not guarantee anything, but this might work as an anti-recoil as it holds the mouse pointer in place when Left Mouse is pressed. Theoretically, it could work.

Code: Select all

#SingleInstance Force
CoordMode, Mouse, Screen
PID := (DllCall("GetCurrentProcessId"))				;PID Of Script.
Process, Priority, %PID%, Above Normal				;Improves Performance Of Script.
SetBatchLines, -1									;Improves Performance Of Script.
~LButton::
{	MouseGetPos, x, y
	While, (GetKeyState("LButton", "P"))
	{	MouseMove, %x%, %y%
}	}	Return
~Esc::ExitApp
Also, If you want the pointer to go Left you just need a larger number:

Code: Select all

#SingleInstance Force
~LButton::
While, GetKeyState("LButton", "P")
{	MouseXY(-10,7) ;x=-10, y=+7, the bigger the number the harder the pull. (X= -=Left, +=Right) (Y= -=Up, +=Down)
	MouseXY(x,y)
	{	DllCall("mouse_event",int,1,int,x-10,int,y+7,uint,0,uint,0)
	} Sleep, 10
} Return
kjaty
Posts: 36
Joined: 10 Dec 2019, 22:02

Re: I need help improving an AHK.

15 Dec 2019, 19:52

PipeDreams wrote:
15 Dec 2019, 15:01
A few things:
I am no AHK god, there are a lot of people that know waaaaaaaaaaaaaaayyyy more than I do about AHK lol, but thanks for the compliment.

This work fine on my end, you just had the HotKey wrong, it should look like this

Code: Select all

~LButton::
not this

Code: Select all

~ LButton ::

Code: Select all

~LButton::
While, GetKeyState("LButton", "P")
{	mouseXY(0,7)
	sleep 1
	mouseXY(0,4)
	sleep 1
	mouseXY(0,-1)
	sleep 5
	mouseXY(-1,0) ; go left 
	sleep 5
	mouseXY(1,0) ; go left 
	sleep 5
	
	mouseXY(x,y)
	{	DllCall("mouse_event",int,1,int,x,int,y,uint,0,uint,0)
	}
} Return
I tried however unsuccessfully, even after the change you indicated me still after about 5 seconds with the left button pressed it starts to rise again.
I was thinking of something like, I press the left button and after 5 seconds I'm still pressed the system restarts the loop every 5 seconds so that while I'm pressing it is restarting the command and pulling the mouse down.
kjaty
Posts: 36
Joined: 10 Dec 2019, 22:02

Re: I need help improving an AHK.

15 Dec 2019, 19:56

It would be the same idea of this code that I found in the forum, but instead of using the up key I would use the left mouse button.

Code: Select all

Ligado_Up := 0
Ligado_Down := 0

; COMANDOS PARA CIMA

$Up::
If (Ligado_Up = 0)
{
	SetTimer, Pressionar_Para_Cima, 1000
	Ligado_Up := 1
}
Sleep 1000
Return

$Up up::
SetTimer, Pressionar_Para_Cima, Off
If ((A_TickCount - Ultimo_Comando_Up) >= 500)
{
	Send {Up}
}
Ligado_Up := 0
Return


Pressionar_Para_Cima:
Send {Up}
Ultimo_Comando_Up := A_TickCount
Return

; COMANDOS PARA BAIXO

$Down::
If (Ligado_Down = 0)
{
	SetTimer, Pressionar_Para_Baixo, 1000
	Ligado_Down := 1
}
Sleep 1000
Return

$Down Up::
SetTimer, Pressionar_Para_Baixo, Off
If ((A_TickCount - Ultimo_Comando_Down) >= 500)
{
	Send {Down}
}
Ligado_Down := 0
Return


Pressionar_Para_Baixo:
Send {Down}
Ultimo_Comando_Down := A_TickCount
Return
[Mod edit: [code][/code] tags added again.]
kjaty
Posts: 36
Joined: 10 Dec 2019, 22:02

Re: I need help improving an AHK.

15 Dec 2019, 20:23

PipeDreams wrote:
15 Dec 2019, 15:01
A few things:
I am no AHK god, there are a lot of people that know waaaaaaaaaaaaaaayyyy more than I do about AHK lol, but thanks for the compliment.

This work fine on my end, you just had the HotKey wrong, it should look like this

Code: Select all

~LButton::
not this

Code: Select all

~ LButton ::

Code: Select all

~LButton::
While, GetKeyState("LButton", "P")
{	mouseXY(0,7)
	sleep 1
	mouseXY(0,4)
	sleep 1
	mouseXY(0,-1)
	sleep 5
	mouseXY(-1,0) ; go left 
	sleep 5
	mouseXY(1,0) ; go left 
	sleep 5
	
	mouseXY(x,y)
	{	DllCall("mouse_event",int,1,int,x,int,y,uint,0,uint,0)
	}
} Return

Also, look out for stuff like this:

Code: Select all

mouseXY (-1,0); go left ;<<< You need to have one space between the ) and ; or the script will not work correctly. 
 sleep 5
mouseXY (1,0), go left ;<<< You can only comment with a semicolon, not a comma; the extra comma is causing an issue.

I can not guarantee anything, but this might work as an anti-recoil as it holds the mouse pointer in place when Left Mouse is pressed. Theoretically, it could work.

Code: Select all

#SingleInstance Force
CoordMode, Mouse, Screen
PID := (DllCall("GetCurrentProcessId"))				;PID Of Script.
Process, Priority, %PID%, Above Normal				;Improves Performance Of Script.
SetBatchLines, -1									;Improves Performance Of Script.
~LButton::
{	MouseGetPos, x, y
	While, (GetKeyState("LButton", "P"))
	{	MouseMove, %x%, %y%
}	}	Return
~Esc::ExitApp
Also, If you want the pointer to go Left you just need a larger number:

Code: Select all

#SingleInstance Force
~LButton::
While, GetKeyState("LButton", "P")
{	MouseXY(-10,7) ;x=-10, y=+7, the bigger the number the harder the pull. (X= -=Left, +=Right) (Y= -=Up, +=Down)
	MouseXY(x,y)
	{	DllCall("mouse_event",int,1,int,x-10,int,y+7,uint,0,uint,0)
	} Sleep, 10
} Return
I tried the above help, but to no avail, even after the change you indicated me still after about 5 seconds with the left button pressed it starts to rise again.
I was thinking of something like, I press the left button and after 5 seconds I'm still pressed the system restarts the loop every 5 seconds so that while I'm pressing it is restarting the command and pulling the mouse down.

I posted a code that appeared with what I wanted, in the case of the code I posted it basically does it:

"As long as I keep pressing the up arrow the system will send the keyup command at 1 second intervals and while holding the down arrow the system will send the keydown command with 1 second intervals. 1 second".
gregster
Posts: 8988
Joined: 30 Sep 2013, 06:48

Re: I need help improving an AHK.

16 Dec 2019, 01:37

@kjaty: Please start using code tags when posting code: [code]code goes here[/code] (by now, they have been added multiple times for you). You can use the fifth button from the left in the 'Full Editor' to add them. Thank you!
If you have questions about this forum feature, don't hesitate to ask.
User avatar
PipeDreams
Posts: 165
Joined: 19 Dec 2015, 00:20

Re: I need help improving an AHK.

16 Dec 2019, 04:19

kjaty wrote:
15 Dec 2019, 20:23
I tried the above help, but to no avail, even after the change you indicated me still after about 5 seconds with the left button pressed it starts to rise again.

I was thinking of something like, I press the left button and after 5 seconds I'm still pressed the system restarts the loop every 5 seconds so that while I'm pressing it is restarting the command and pulling the mouse down.
What? Does this only happen in-game when you shoot your gun? Can you replicate the issue if you just run the script on your desktop? Because when I run it, I have zero problems, and the pointer only goes where I have directed. I have no idea why that would happen unless the recoil of the gun steadily increases over time witch would result in canceling out the script after 5 seconds because the upward pull of the guns recoil is greater than the downward pull of the script. If that is the case, then you will need a script that slowly increases the downward pull to exceed the recoil on the gun, and that is a bit harder to do, especially when different weapons have different recoil settings.


kjaty wrote:
15 Dec 2019, 20:23
I posted a code that appeared with what I wanted, in the case of the code I posted it basically does it:

"As long as I keep pressing the up arrow the system will send the keyup command at 1 second intervals and while holding the down arrow the system will send the keydown command with 1 second intervals. 1 second".
Where? I don't see any code.
Also, there is no need to "restart the script to restart the loop," loops automatically restart themselves, hence the name LOOP.
kjaty
Posts: 36
Joined: 10 Dec 2019, 22:02

Re: I need help improving an AHK.

16 Dec 2019, 07:45

Code: Select all

Thanks, I'm new here on the forum and got lost.
gregster wrote:
16 Dec 2019, 01:37
@kjaty: Please start using code tags when posting code: [code]code goes here[/code] (by now, they have been added multiple times for you). You can use the fifth button from the left in the 'Full Editor' to add them.
If you have questions about this forum feature, don't hesitate to ask.
gregster
Posts: 8988
Joined: 30 Sep 2013, 06:48

Re: I need help improving an AHK.

16 Dec 2019, 07:49

kjaty wrote:
16 Dec 2019, 07:45

Code: Select all

Thanks, I'm new here on the forum and got lost.
No problem.

Yes, that's the way to use the code tags - but it will be sufficient, if you use them around script code ;). Thank you.
(like here, for example: https://www.autohotkey.com/boards/viewtopic.php?f=18&t=70633&p=305942#p305874 )
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: I need help improving an AHK.

16 Dec 2019, 10:39

@gregster
How does one link to a specific post rather than just to the thread?
kjaty
Posts: 36
Joined: 10 Dec 2019, 22:02

Re: I need help improving an AHK.

16 Dec 2019, 13:14

@PipeDreams
Sorry I still get lost in this forum.
It would be for just one weapon, would you have any idea how to implement some code for this?
If you want to take a look at the code, follow below:

Code: Select all

Ligado_Up := 0
Ligado_Down := 0

; COMANDOS PARA CIMA

$Up::
If (Ligado_Up = 0)
{
	SetTimer, Pressionar_Para_Cima, 1000
	Ligado_Up := 1
}
Sleep 1000
Return

$Up up::
SetTimer, Pressionar_Para_Cima, Off
If ((A_TickCount - Ultimo_Comando_Up) >= 500)
{
	Send {Up}
}
Ligado_Up := 0
Return


Pressionar_Para_Cima:
Send {Up}
Ultimo_Comando_Up := A_TickCount
Return

; COMANDOS PARA BAIXO

$Down::
If (Ligado_Down = 0)
{
	SetTimer, Pressionar_Para_Baixo, 1000
	Ligado_Down := 1
}
Sleep 1000
Return

$Down Up::
SetTimer, Pressionar_Para_Baixo, Off
If ((A_TickCount - Ultimo_Comando_Down) >= 500)
{
	Send {Down}
}
Ligado_Down := 0
Return


Pressionar_Para_Baixo:
Send {Down}
Ultimo_Comando_Down := A_TickCount
Return
Last edited by kjaty on 16 Dec 2019, 16:42, edited 1 time in total.
gregster
Posts: 8988
Joined: 30 Sep 2013, 06:48

Re: I need help improving an AHK.

16 Dec 2019, 13:47

@kjaty: I'll try to have a look later.

@Hellbent : It depends on the forum theme, afaik. In "Digi", that I use, you can click the individual (usually light blue, sometimes white) title of each post and use the corresponding URL that appears in the browser address field (or directly copy that link via the right-click-menu of that title). But a theme like 'Simplicity' doesn't seem to show these post titles - there you can use the date link at the top of each post instead. I haven't checked the other themes... but probably similarly.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 49 guests