Changing AHK Response, Everytime I move my mouse :( - autohotkey

Here's my sample script
$f2::
loop, 1
while GetKeyState("f2", "P")
{
setkeydelay, 1
send, {f2}
click
}
return
I have 3 delays Normal, Fast, Faster everytime I move my mouse it changes its delay.What should I add in the script to make the response consistent?

ListLines, Off
CoordMode, Mouse
cnt:=1, arr:=[50 ; normal
, 25 ; fast
, 0] ; faster
$F2::
;~ TrayTip,, % "delay is: "arr[cnt]
MouseGetPos, xPos, yPos
xPosPrev:=xPos, yPosPrev:=yPos
While, GetKeyState("F2", "P")
{
MouseGetPos, xPos, yPos
If (xPosPrev!=xPos Or yPosPrev!=yPos)
{
`(cnt=3) ? cnt:=1:cnt++, xPosPrev:=xPos, yPosPrev:=yPos, bool:=False
;~ TrayTip,, % "delay changed to: "arr[cnt]
While, !bool
{
MouseGetPos, xPos, yPos
bool:=(xPosPrev=xPos And yPosPrev=yPos) And A_Index>50 ? True:False
Sleep, GetKeyState("F2", "P") ? 25:-1
xPosPrev:=xPos, yPosPrev:=yPos
}
}
SetKeyDelay, arr[cnt]
Send, {F2}{Click}
Sleep, GetKeyState("F2", "P") ? 25:-1
}
Return

Related

Script modification(drag and drop)

I have simple script that looks for specified color pixel in restricted area then performs move mouse of this pixel to specified x,y coordinates it looks like this(control hold is added, because it needs to be pressed while moving stock):
#ifwinactive
F10::
PixelSearch, ItemX, ItemY, 1800, 600, 1700, 500, 0xFFE047, 1 , RGB FAST
if ErrorLevel = 0
Mousemove, ItemX, ItemY, 0
Send, {lbutton down}
sleep, 20
Send, {lctrl down}
Random x, 1600, 1700
Random y, 590, 655
Mousemove, x, y, 0
Send, {lbutton up}
Send, {lctrl up}
return
It works great but takes control over my MOUSE which I don't want, so I am trying to modify it to perform action without taking control over my mouse. I have tried to search, using controlclick but this couldn't handle dragging, then I have read about PostMessage which I don't clearly understand but I have come with this kind of script(not fully understand dll calls that's why most likely it doesn't work like intented):
#ifwinactive
x := 1650
y := 620
F10::
PixelSearch, ItemX, ItemY, 1900, 600, 1750, 500, 0xFFC661, 1, RGB Fast
CoordMode, Pixel, Screen
if !hWnd := DllCall("WindowFromPoint", Int,ItemX, Int,ItemY, Ptr)
if !hWnd := DllCall("WindowFromPoint", UInt64,(ItemX&0xFFC661)|(ItemY<<32), Ptr)
if !hWnd := DllCall("WindowFromPoint", UInt64, x|(y << 32), Ptr)
return
VarSetCapacity(POINT, 8, 0)
NumPut(ItemX, POINT, 0, "Int"), NumPut(ItemY, POINT, 4, "Int")
DllCall("user32\ScreenToClient", Ptr,hWnd, Ptr,&POINT)
ItemX := NumGet(POINT, 0, "Int"), ItemY := NumGet(POINT, 4, "Int")
if ErrorLevel
PostMessage, 0x0200, 0, ItemX&0xFFC661|ItemY<<16,, % "ahk_id " hWnd ;WM_MOUSEMOVE
sleep, 5
PostMessage, 0x0201, 0, ItemX&0xFFC661|ItemY<<16,, % "ahk_id " hWnd ;WM_LBUTTONDOWN
sleep, 5
PostMessage, 0x0200, 0, x|y<<16,, % "ahk_id " hWnd ;WM_MOUSEMOVE
sleep, 5
PostMessage, 0x0202, 0, x|y<<16,, % "ahk_id " hWnd ;WM_LBUTTONUP
sleep, 5
return
There is problem with this scirpt it actually act like this: Search for pixel>if it's there>perform post messages under my cursor(actually taking things under my mouse to specified x,y.
While what I want is to look for pixel, pick this pixel and move it. Is it even possible without taking control over my mouse?
EDIT!
NEW CODE
#ifwinactive
CoordMode, Pixel , Screen
x := 1650
y := 620
lparam := pX | (pY << 16)
lparam2 := x | (y << 16)
f10::
PixelSearch, pX, pY, 1900, 1000, 1750, 500, 0xFFE047, 1, Fast
if Errorlevel
{
PostMessage, 0x0200, 0, %lparam%,, ahk_exe something.exe ;WM_MOUSEMOVE
sleep, 10
PostMessage, 0x0201, 0x0001, %lparam%,, ahk_exe something.exe ;WM_LBUTTONDOWN
sleep, 10
PostMessage, 0x0200, 0x0001, %lparam2%,, ahk_exe something.exe ;WM_MOUSEMOVE
sleep, 10
PostMessage, 0x0202, 0, %lparam2%,, ahk_exe something.exe ;WM_LBUTTONUP
}
return

How do i stop AHK loop when LButton is up

I just want this loop to work when LButton is down and stop when LButton is up.I
The code works when LButton is down but it continues working when i lift my finger off the left click button.
mem:=0
~*LButton::
Sleep, 100
KeyWait, LButton, T0.10
If ErrorLevel = 1
{
While GetKeyState("LButton","P")
Loop {
GetKeyState, state, LButton, P
If state = U
Break
MouseGetPos, xpos, ypos
if (xpos > mem) ;moved right
{
send, {a down} ;send key
mem:=xpos
}
else
send, {a up}
if (xpos < mem) ;moved left
{
send, {d down}
mem:=xpos
}
else
send, {d up}
Sleep, 100
}
}
return
You case is very similar to the example of the usage of the while loop.
If you look at that you can see, you don't need the Loop inside the while. Just have
while GetKeyState("LButton")
{
; Your code here:
MouseGetPos, xpos, ypos
if (xpos > mem) ;moved right
{
send, {a down} ;send key
mem:=xpos
}
else{
send, {a up}
}
if (xpos < mem) ;moved left
{
send, {d down}
mem:=xpos
}
else{
send, {d up}
}
Sleep, 100
}

How to Send key faster with PixelGetColor / PixelSearch

I am using this code to send pot key when I press skill key
*RButton::
{
PixelGetColor, ColorOutPut, 256, 762, Fast RGB ;2 last
if (ColorOutPut = 0x090606)
{
;~ PixelGetColor, ColorOutPut, 273, 750, Fast RGB ;charge
;~ if not (ColorOutPut = 0x3A3436)
;~ {
SetKeyDelay -1,-1
Sendinput {Blind}{2 down}
SetKeyDelay -1,-1
Sendinput {Blind}{2 up}
;~ }
}
SetKeyDelay -1,-1
SendInput, {Blind}{RButton DownR}
}
return
*RButton up::
SetKeyDelay -1,-1
SendInput, {Blind}{RButton up}
return
PixelGetColor run time is 0.1-0.2 sec for me so skill is being late that time, because of it i can't send multiple PixelGetColor.
What I want to ask is how can I read color and send keys instantly and that way my skill won't be late?
And I cant send skill forward because skill gets help from pots. Thx for you support.
You may want to add 'Process, Priority,, High'. The -1/-1 are not ideal to use, most people use 'SetKeyDelay, 0'

Click and drag one pixel in given direction

I am looking for a script that will click and hold the right mouse button down and drag in a given direction 1 pixel every "x" or 2 seconds. Something I can either tell to move in given direction by hitting the corresponding direction key or by adjusting the script manually.
Thank you!
Not entirely sure what you want, but try this:
CoordMode, Pixel, Screen
direction = left
secondsBetweenMoves = 0.1
F1:: ;F1 to start it
SendInput, {LButton Down}
SetTimer, Move, %secondsBetweenMoves%
Return
F2:: ;F2 to end it
SendInput, {LButton Up}
SetTimer, Move, Off
Return
Move:
MouseGetPos, mouseX, mouseY
If (direction = "left") {
MouseMove, mouseX-1, mouseY
}
Else If (direction = "right") {
MouseMove, mouseX+1, mouseY
}
Else If (direction = "up") {
MouseMove, mouseX, mouseY-1
}
Else If (direction = "down") {
MouseMove, mouseX, mouseY+1
}
Return
The send event command might make your life a whole lot easier. For example, what you're trying to do in a simplistic infinite loop:
coordmode, mouse, screen
setmousedelay, 0 # This makes the mouse move extremely fast
loop {
mousegetpos, mx, my
mx := mx+1
sendevent {click, r, down}
sendevent {click, %mx%, %my%, r, up}
sleep 2000
}
This loop will hold down the right button and move one pixel to the right every two seconds. If you want it to move to the left, change
mx := mx+1
to
mx := mx-1
Making it move up or down is the same mechanism, but adding or subtracting to the variable "my" instead.
This loop can be made into a timer, with hotkeys to enable or disable it.

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
}