AutoHotKey Clicking Script - autohotkey

I've only just started with AutoHotKey, and I'm looking to make a script that will click once per second 10 times, then hold right mouse button for 3 seconds, before resetting. I intend it to active on alt+c, and break if I press the left mouse button.
The script I came up with it
LButton::
BreakLoop = 1
return
!c::
Loop
{
if (BreakLoop = 1)
break
;
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Send, {RButton Down}
Sleep, 3000
Send, {RButton Up}
Return
}
However, this is not working. Is there a way to fix this, or have I taken the completely wrong approach for this script?

You did make a Mistake in the code, On the Bottom You did have the Return Command into the Loop that is not possible. (This Return Command Will be needed for !c:: and it must outsite the loop command)
The Code must be Like:
~LButton::
BreakLoop = 1
return
!c::
Loop
{
if (BreakLoop = 1)
break
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Click
Sleep, 1000
Send, {RButton Down}
Sleep, 3000
Send, {RButton Up}
}
Return
Tip: if you change Lbutton:: into ~Lbutton:: then The Default LeftButton is also active.

I was actually able to find a way to compact it significantly (and break the loop faster) by nesting a loop within a loop
!s::
BreakLoop = 1
return
!c::
BreakLoop = 0
Loop
{
Loop 10
{
if (BreakLoop = 1)
break
;
Click
Sleep, 900
}
Send, {RButton Down}
Sleep, 3000
Send, {RButton Up}
}
if (BreakLoop = 1)
Break
;
Return

A better method is to use SetTimer, this allows you to break out of the loop at any point in your sequence of actions.
Try:
!c::setTimer, doAction, 1000
!s::SetTimer, doAction, Off
doAction:
i += (i <= 14 ? 1 : -13)
if (i == 14)
send, {RButton Up}
else if (i == 11 )
Send, {RButton Down}
else if (i <= 10)
click
return

Related

Autohotkey, problems redefining keypress down state of mousekey

I am using the LButton (mouse left) in a keybind as a prefix key. I got it to work, problem is I now need to redefine the LButton as whatever it was in it's natural state...in Autohotkey's terms.
I read this: https://www.autohotkey.com/docs/commands/GetKeyState.htm.
And cameup with the following code, but it's not working at all the way I thought it would. Simply put, you can use $LButton::Send {Click Left} to emulate the basic mouse click. The problem is when you hold the button/key down, nothing happens. I thought the code to emulate, or define the 'pressed down' behaviour would be readily available, but what I've found isn't working.
$LButton::
if (GetKeyState("LButton", "P"))
Send, {Click Left Down} ;tried variants with {Click Left} etc alrdy
else
Send, {Click Left Up}
return
For person in comments:
LButton & ~RButton::
Send, 1{Click Right}{Click Left}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
return
$LButton:: ;no idea what this shud be
SendInput {Click Left}
;Send, {Click Left Down}
;KeyWait, LButton
;Send, {Click Left Up}
return
RButton::
Send, 1{Click Right}{Click Left}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
return
Not sure if I understood this correctly, but you want holding LButton, then clicking the RButton to run this code:
Send, 1{Click Right}{Click Left}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
And just clicking RButton should run that code as well?
And if LButton is just pressed normally (not in combination with RButton) it should function as normal?
Well this would be it:
~LButton & RButton::
RButton::
Send, 1{Click Right}{Click Left}{Click Right}{MButton}
Sleep, 130
Send, 1
return
Basically just making use of the ~ modifier.
Although I can't speak for Send, 1{Click Right}{Click Left}{Click Right}{MButton}.
I don't know what it's supposed to do, but maybe/hopefully it's doing the right thing.
Pressing w or something that you define should do the job
w::
Send, {Click down}
Send, {Blind}1{Click Right}{Click}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
return
Anyone looking for something similar could try in this direction
#SingleInstance force
#warn
r::Reload
x::ExitApp
w::
Send, {Click down}
Send, {Blind}1{Click Right}{Click}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
return
just Pressing w should do the job
Alternately
, the below one will do the same
LButton::Send, {Click down}
RButton::
if GetKeyState("LButton", "P")
{
Send, {Blind}1{Click Right}{Click}{Click Right}{MButton}
Sleep 130 ;125
Send, 1
}
else
Send, {Click Right}
return

Need help on AHK script closing when pixel change

Hello I am fairly new to coding and I’m making a programme for a game I play on AHK, I’m trying to code a script so that when a pixel changes colour on my screen the script will completely stop whilst remaining part of the loop. The main code works but I've tried to add a system where the script will stop running if a pixel changes colour. Thanks for the help in advance.
Esc::ExitApp
+r::
sleep, 1000
Send, {e down}
Send, {e up}
sleep, 10750
loop {
PixelGetColor, color1, 949, 269
if(color1 = “0xA6A6A6”)
sleep, 500
SendInput {Esc}
IfWinActive ahk_exe FiveM_GTAProcess.exe
sleep, 5550
Send, {e down}
Send, {e up}
sleep, 500
MouseClick Left, 649, 390
sleep, 500
MouseClick Left, 964, 484
sleep, 500
MouseClick Left, 1311, 415
sleep, 500
loop, 4
{
MouseClick Left, 964, 484
sleep, 500
}
sleep, 500
MouseClick Left, 720, 415
sleep, 500
}
Add Break after your if:
if(color1 = “0xA6A6A6”)
Break
if(color1 = “0xA6A6A6”)
{
sleep, 500
SendInput {Esc}
}

How to change the loop?

The script is searching for a certain text in the clipboard. When found, it shows a MsgBox.
I would like this script to stop when the text has been found. How to achieve that?
#Persistent
MouseMove, 821, 700
Sleep, 500
MouseClick, Left
Sleep, 500
Loop, 5
{
Send, ^c
Sleep, 500
Send, {PgDn}
}
OnClipboardChange:
If InStr( Clipboard, "Part3" )
SetTimer, PopupMsgBox, -1
Return
PopupMsgBox:
Msgbox, Part3 Found
Return
#Persistent
clip_search := "Part3"
MouseMove, 821, 700
Sleep, 500
MouseClick, Left
Sleep, 500
Loop, 5
{
Send, ^c
ClipWait
If InStr( Clipboard, clip_search )
{
MsgBox, % clip_search " Found."
Break
}
Sleep, 500
Send, {PgDn}
}

AHK - How to hold a macro while Physical Key depressed

What I want to do is this:
Numpad3::
if(not GetKeyState("Shift" , "P") and not GetKeyState("RButton" , "P"))
{
SendInput {Shift down}
Sleep, 33
Click down right
}
Return
Numpad3 Up::
Sleep, 100
Click up right
Sleep, 33
SendInput {Shift up}
Return
But for some reason it isn't canceling when I let the button up. :(
I would suggest to use Send {RButton Down} (or Up) to send the right mouse click, instead of Click up right.
Also, you don't want to be sending random Sleep's if they are not really necessary, as it creates lag and makes the script inelegant and potentially unreadable.
Here is code which sends Control instead of RButton but it's only so I can test it within Notepad++.
Just replace Control with RButton and have a go:
*NumpadPgDn::
*Numpad3::
Send {Shift Down}{Control Down}
return
*NumpadPgDn Up::
*Numpad3 Up::
Send {Shift Up}{Control Up}
return

How To Detect Ctrl+V In AHK?

I am trying to detect Ctrl+V. Then, make Xbutton1act as Enter for a few seconds, but I can't get it working.
Transform, CtrlV, Chr, 3
Input, OutputVar, L1 M
XButton1::
if OutputVar = CtrlV
{
SetTimer, SendEnter, 0
Sleep, 2000
SetTimer, SendEnter, Off
}
else
{
Send ^t
}
Return
SendEnter:
Send {Enter}
Return
~^v::lastPaste := A_TickCount ;stores counter when ctrl+v is pressed
Xbutton1::
If A_TickCount - lastPaste < 2000 ;checks if 2 seconds gone after ctrl+v was clicked
{
Send, {Enter} ;sends enter
Return
}
else
{
Send, ^t ;sends ctrl+t
Return
}