AHK stuck with function with value outside of it - autohotkey

Those if doesn't work, because test() doesn't take value from outside, but I have no clue how I should write code if I put it inside...
Any suggestions?
SetTimer, test, 2000 ;I started off with 1000 then worked up to this number to confirm this was happening
number := 1
test:
test()
return
test(){
if WinActive("program") {
ImageSearch, foundX, foundY, 2000, 0, 2560, 1440, target.png
if (ErrorLevel = 1) {
if (number = 1) {
CheckMap(z := "star.png")
number := 2
}
if (number = 2) {
CheckMap(z := "like.png")
number := 3
}
if (number = 3) {
CheckMap(z := "question.png")
number := 4
}
if (number = 4) {
CheckMap(z := "cross.png")
number := 1
}
}
}
}
CheckMap(x){
ImageSearch, foundX, foundY, 2000, 0, 2560, 1440, %x%
if (ErrorLevel = 0) {
MouseGetPos, StartX, StartY
Click, %foundX%, %foundY%
MouseMove, StartX, StartY
}
}
*[::
Suspend
Pause, 1
return
*]::
ExitApp
return

Found some help, that I needed static number, also fixed if to else if, now works perfect
SetTimer, test, 2000 ;
test:
test()
return
test(){
static number := 1
if WinActive("program") {
ImageSearch, foundX, foundY, 2000, 0, 2560, 1440, target.png
if (ErrorLevel = 1) {
if (number = 1) {
CheckMap(z := "star.png")
number := 2
} else if (number = 2) {
CheckMap(z := "like.png")
number := 3
} else if (number = 3) {
CheckMap(z := "question.png")
number := 4
} else if (number = 4) {
CheckMap(z := "cross.png")
number := 1
}
}
}
}
CheckMap(x){
ImageSearch, foundX, foundY, 2000, 0, 2560, 1440, %x%
if (ErrorLevel = 0) {
MouseGetPos, StartX, StartY
Click, %foundX%, %foundY%
MouseMove, StartX, StartY
}
}
*[::
Suspend
Pause, 1
return
*]::
ExitApp
return

Related

Moving the cursor to a specified position as soon as it touches right edge of screen using autohotkey?

I have written code to change mouse position when it touches the left edge of screen
sysget, var_, monitorworkarea
gui, color, 0x000000
gui, +alwaysontop +toolwindow -caption
gui, show, w1 h%var_bottom% x0 y0, left_win
winset, transparent, 1, left_win
onmessage(0x200, "edge")
return
edge()
{
static countdown := 0
if (countdown = 0)
{
settimer, check, -100
countdown := 1
}
return
check:
{
mousegetpos, , , win
wingettitle, title, ahk_id %win%
if (title = "left_win")
{
MouseMove, 960, 300, 0
}
countdown := 0
}
return
}
but it doesn't work if I replace left with right for right edge of screen How do I fix this ?
Try this
#Persistent
SetTimer, check, -100
return
check(){
If edge("left")
{
MouseMove, 960, 300, 0
Send #x
}
else if edge("right")
{
MouseMove, 960, 300, 0
Run Notepad
}
; ...
SetTimer, check, -100
}
edge(pos){
SysGet, VirtualWidth, 78
SysGet, VirtualHeight, 79
T := 10
CoordMode, Mouse, Screen
MouseGetPos, mX, mY
left := (mX < T)
top := (mY < T)
right := (mX > VirtualWidth - T)
bottom := (mY > VirtualHeight - T)
If (pos = "left")
return left
else if (pos = "top")
return top
else if (pos = "right")
return right
else if (pos = "bottom")
return bottom
}

AHK PixelSearch Loop

I'm trying to get my AHK Script to Work.
Basically i want to find a row of x Pixels with the Color 0x26FDFD (BGR). But i don't know the AHK Scripting language well wnough to think about a smart, clean and easy way to program that loop, that the starting point will be modified based on the last found coordinates.
However, here's what i got so far:
SysGet, VirtualWidth, 78
SysGet, VirtualHeight, 79
;Farbe nach der gesucht wird:
ColorVar := 0x26FDFD
i:=0
while i < 10
{
PixelSearch, FoundX, FoundY, 0, 0, VirtualWidth, VirtualHeight, %ColorVar%, 3, Fast
if (ErrorLevel = 2)
{
MsgBox Could not conduct the search.
return
}
else if (ErrorLevel = 1)
{
MsgBox Color could not be found on the screen.
return
}
else
{
MouseMove, %FoundX%, %FoundY%
MsgBox Found a Pixel at %FoundX%x%FoundY%.
;return
}
i++
}
Kinda stupid and basic question, but somehow i can't figure it out.
I just needed to store the X and Y coordinates at the end of each loop, then set the new startpoint accordingly.
here's the code:
i:=0
while i < 5
{
PixelSearch, FoundX, FoundY, startX%A_Index%, startY%A_Index%, VirtualWidth, VirtualHeight, %ColorVar%, 3, Fast
Switch ErrorLevel
{
Case 1:
MsgBox Website ueberpruefen!
return
Case 2:
MsgBox Makro ueberpruefen!
return
Default:
MouseMove, %FoundX%, %FoundY%
;MsgBox Found a Pixel at %FoundX%x%FoundY%.
nextLoop := A_Index + 1
startX%nextLoop% := FoundX + 1
startY%nextLoop% := FoundY
}
i++
}
;msgbox % "found 5 matches!, first at: " startX2 "x" startY2
return

How do you convert or Abbreviate Numbers in Autohotkey(AHK)? (posting answers made by the autohotkey discord group)

I needed to convert numbers in Autohotkey for a game I'm making and some members of the Autohotkey Discord Group were able to help me out.
In particular vieira and andreas#Nasa:~$ sudo -i each came up with a working solution.
vieira
print(GetFormatedNum(1234567890))
print(GetFormatedNum(1234567))
print(GetFormatedNum(1234))
print(GetFormatedNum(12))
GetFormatedNum(num) {
for k, v in [{12:"T"},{9:"B"},{6:"M"},{3:"K"}] {
for i, j in v
if (num >= (10**i))
return % SubStr(num/(10**i),1,5) j
}
return num
}
1.234B
1.234M
1.234K
12
andreas#Nasa:~$ sudo -i
InputBox, num, Num Input, Input the number you want to be converted
if num is not Integer
return
MsgBox, % "Num is: " . num
MsgBox, % "this is converted: " . Converter.Convert(num)
return
class Converter {
static 1 := "k"
static 2 := "M"
static 3 := "G"
static 4 := "T"
Convert(int){
if int is not Integer
Throw, Exception("Illegal type", -1)
size := Floor(Floor(Log(int)) / 3)
if(size == 0)
return int
While(true){
Try {
ending := this[size]
break
}
Catch e {
if(e.Message == "key to great")
size--
}
}
return, Round(Floor(int / (10 ** (size * 3 - 1)))/ 10, 1) . Ending
}
__Get(vKey){
if(vKey > 0)
Throw, Exception("key to great")
return, 0
}
}
I am immensely grateful to each of them and to BoBo and elmodo7 for helping me this morning.
andreas#Nasa:~$ sudo -i
InputBox, num, Num Input, Input the number you want to be converted
if num is not Integer
return
MsgBox, % "Num is: " . num
MsgBox, % "this is converted: " . Converter.Convert(num)
return
class Converter {
static 1 := "k"
static 2 := "M"
static 3 := "B"
static 4 := "T"
Convert(int){
if int is not Integer
Throw, Exception("Illegal type", -1)
size := Floor(Floor(Log(int)) / 3)
if(size == 0)
return int
While(true){
Try {
ending := this[size]
break
}
Catch e {
if(e.Message == "key to great")
size--
}
}
return, Round(Floor(int / (10 ** (size * 3 - 1)))/ 10, 1) . Ending
}
__Get(vKey){
if(vKey > 0)
Throw, Exception("key to great")
return, 0
}
}
edit: personally all this is beyond me but this solution is his final answer.
conv := new Converter()
Loop, 10 {
Random, num, 0, 2147483647
num := num * 1000000
Print("Num is: " . num . " and this is converted: " . conv.Convert(num))
}
return
class Converter {
__New(){
this.endingChars := new EndChars("k", "M", "G", "T") ; put the endings for each step here in the correct order...
}
Convert(int){
if int is not Integer
Throw, Exception("Illegal type", -1)
size := Floor(Floor(Log(int)) / 3)
if(size == 0)
return int
While(size > 0){
Try {
ending := this.endingChars[size]
break
}
Catch e {
size--
}
}
return, Round(Floor(int / (10 ** (size * 3 - 1)))/ 10, 1) . ending
}
}
class EndChars {
__New(EndingChars*){
for k, i in EndingChars
this[k] := i
}
__Get(vKey){
if(vKey > 0)
Throw, Exception("key to great")
return, 0
}
}
and you can just add the next character in line to EndChars

Display effect when double left mouse button click

There is a script that makes the effect when I click mouse buttons.
Code of this script.
#NoEnv
CoordMode Mouse, Screen
Setup()
~LButton::ShowRipple(LeftClickRippleColor)
~MButton::ShowRipple(MiddleClickRippleColor)
~RButton::ShowRipple(RightClickRippleColor)
Setup()
{
Global
RippleWinSize := 170
RippleStep := 4
RippleMinSize := 10
RippleMaxSize := RippleWinSize - 20
RippleAlphaMax := 0x4147
RippleAlphaStep := RippleAlphaMax // ((RippleMaxSize - RippleMinSize) / RippleStep)
RippleVisible := False
LeftClickRippleColor := 0xff0000
MiddleClickRippleColor := 0xff00ff
RightClickRippleColor := 0xffa500
DllCall("LoadLibrary", Str, "gdiplus.dll")
VarSetCapacity(buf, 16, 0)
NumPut(1, buf)
DllCall("gdiplus\GdiplusStartup", UIntP, pToken, UInt, &buf, UInt, 0)
Gui Ripple: -Caption +LastFound +AlwaysOnTop +ToolWindow +Owner +E0x80000
Gui Ripple: Show, NA, RippleWin
hRippleWin := WinExist("RippleWin")
hRippleDC := DllCall("GetDC", UInt, 0)
VarSetCapacity(buf, 40, 0)
NumPut(40, buf, 0)
NumPut(RippleWinSize, buf, 4)
NumPut(RippleWinSize, buf, 8)
NumPut(1, buf, 12, "ushort")
NumPut(32, buf, 14, "ushort")
NumPut(0, buf, 16)
hRippleBmp := DllCall("CreateDIBSection", UInt, hRippleDC, UInt, &buf, UInt, 0, UIntP, ppvBits, UInt, 0, UInt, 0)
DllCall("ReleaseDC", UInt, 0, UInt, hRippleDC)
hRippleDC := DllCall("CreateCompatibleDC", UInt, 0)
DllCall("SelectObject", UInt, hRippleDC, UInt, hRippleBmp)
DllCall("gdiplus\GdipCreateFromHDC", UInt, hRippleDC, UIntP, pRippleGraphics)
DllCall("gdiplus\GdipSetSmoothingMode", UInt, pRippleGraphics, Int, 4)
MouseGetPos _lastX, _lastY
SetTimer MouseIdleTimer, 5000
Return
MouseIdleTimer:
MouseGetPos _x, _y
if (_x == _lastX and _y == _lastY)
ShowRipple(MouseIdleRippleColor, _interval:=20)
else
_lastX := _x, _lastY := _y
Return
}
ShowRipple(_color, _interval:=10)
{
Global
if (RippleVisible)
Return
RippleColor := _color
RippleDiameter := RippleMinSize
RippleAlpha := RippleAlphaMax
RippleVisible := True
MouseGetPos _pointerX, _pointerY
SetTimer RippleTimer, % _interval
Return
RippleTimer:
DllCall("gdiplus\GdipGraphicsClear", UInt, pRippleGraphics, Int, 0)
if ((RippleDiameter += RippleStep) < RippleMaxSize) {
DllCall("gdiplus\GdipCreatePen1", Int, ((RippleAlpha -= RippleAlphaStep) << 24) | RippleColor, float, 3, Int, 2, UIntP, pRipplePen)
DllCall("gdiplus\GdipDrawEllipse", UInt, pRippleGraphics, UInt, pRipplePen, float, 1, float, 1, float, RippleDiameter - 1, float, RippleDiameter - 1)
DllCall("gdiplus\GdipDeletePen", UInt, pRipplePen)
}
else {
RippleVisible := False
SetTimer RippleTimer, Off
}
VarSetCapacity(buf, 8)
NumPut(_pointerX - RippleDiameter // 2, buf, 0)
NumPut(_pointerY - RippleDiameter // 2, buf, 4)
DllCall("UpdateLayeredWindow", UInt, hRippleWin, UInt, 0, UInt, &buf, Int64p, (RippleDiameter + 5) | (RippleDiameter + 5) << 32, UInt, hRippleDC, Int64p, 0, UInt, 0, UIntP, 0x1FF0000, UInt, 2)
Return
}
How this script to work:
I often do double click left mouse button. I want to have the same effect on my gif was and when I double click left mouse button.
Could anyone of you tell what needs to be done to effect other color, when I make double click left mouse button?
Thanks.
First variant:
~LButton::
if(A_PriorHotkey = "~LButton" && A_TimeSincePriorHotkey < 200){
RippleVisible := False
ShowRipple(0x2E0854)
} else {
ShowRipple(LeftClickRippleColor)
}
return
When I click left mouse button shows the color set for left mouse button, but double-click to change the color to the color for double-click.
Second variant:
DoubleClickWait := 200
~LButton::
SetTimer, SingleClick, Off
if(A_PriorHotkey = "~LButton" && A_TimeSincePriorHotkey < DoubleClickWait){
ShowRipple(0x2E0854)
} else {
SetTimer, SingleClick, -%DoubleClickWait%
}
return
SingleClick:
ShowRipple(LeftClickRippleColor)
return
Ripple effect are make after DelayTime (200 ms in example). If within 200 ms after the first mouse click will not be second click will show the color for a single click. If 200 ms is committed another click, will show the color for the double-click.
Many thanks Capn Odin AutoHotkey user.

Passing an Array through Function Then Looping

I am writing a function that would loop imagesearch but I am having trouble figuring out how to pass a dynamic variable with the options allowed with Arrays (Such as Array0 which retrieves the total count of records in array, and Array%A_Index% which when used with a Loop displays each name as it goes through the list)
arrowList = C:\AHK\LeftArrow.png|C:\AHK\LeftArrow1.png|C:\AHK\GreenLeftArrow.png
StringSplit, arrowArray, arrowList, |
buildList = C:\AHK\build1.png|C:\AHK\build2.png|C:\AHK\build3.png|C:\AHK\build4.png|C:\AHK\build5.png
StringSplit, buildArray, buildList, |
SearchArray("arrowArray","buildArray")
SearchArray(ByRef x, ByRef y)
{
Loop, %x%
{
x2get := %xA_Index%
ImageSearch, imageX, imageY, 0, 0, A_ScreenWidth, A_ScreenHeight, *25 %x2get%
tooltip, searching for %x2get% , 0, 0
If ErrorLevel = 0
{
Loop, % y%0%
{
y2get := % y%A_Index%
ImageSearch, imageX, imageY, 0, 0, A_ScreenWidth, A_ScreenHeight, *25 %y2get%
tooltip, searching for %y2get% , 0, 0
If ErrorLevel = 0
{
MouseClick, Left, imageX, imageY,
Sleep 1000
}
}
}
}
}
You have some problems with how you're calling the variables. You're not actually using "real" arrays there, you're using what's called "pseudo-arrays". You can read about them in the documentation, here.
They're an old way AHK handled arrays, and I strongly suggest you try to move over to using "real" arrays in AHK. You should also update your version of AHK to the latest if you haven't already - http://ahkscript.org/download/.
I changed how the script called some variables, and it should work now, try this, note I've commented the lines I changed:
arrowList = C:\AHK\LeftArrow.png|C:\AHK\LeftArrow1.png|C:\AHK\GreenLeftArrow.png
StringSplit, arrowArray, arrowList, |
buildList = C:\AHK\build1.png|C:\AHK\build2.png|C:\AHK\build3.png|C:\AHK\build4.png|C:\AHK\build5.png
StringSplit, buildArray, buildList, |
SearchArray("arrowArray", "buildArray")
SearchArray(ByRef x, ByRef y)
{
Loop, % %x%0 ; Changed this line
{
x2get := %x% A_Index ; Changed this line
ImageSearch, imageX, imageY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *25 %x2get% ; Changed this line
tooltip, searching for %x2get% , 0, 0
If ErrorLevel = 0
{
Loop, % %y%0 ; Changed this line
{
y2get := % %y% A_Index ; Changed this line
ImageSearch, imageX, imageY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *25 %y2get% ; Changed this line
tooltip, searching for %y2get% , 0, 0
If ErrorLevel = 0
{
MouseClick, Left, %imageX%, %imageY% ; Changed this line
Sleep 1000
}
}
}
}
}
If you're interested in how a solution using "real" arrays would look, here's an example of that. Just make sure you're running the latest version of AHK before you try it, otherwise it might fail.
arrowList := "C:\AHK\LeftArrow.png|C:\AHK\LeftArrow1.png|C:\AHK\GreenLeftArrow.png"
arrowArray := StrSplit(arrowList, "|")
buildList := "C:\AHK\build1.png|C:\AHK\build2.png|C:\AHK\build3.png|C:\AHK\build4.png|C:\AHK\build5.png"
buildArray := StrSplit(buildList, "|")
SearchArray(arrowArray, buildArray)
SearchArray(firstArray, secondArray) {
; Iterate through the first array
for outerIndex, outerValue in firstArray {
; outerIndex = Index of the current element; 1, 2, etc...
; outerValue = The value of the string at that index
Tooltip, Searching for %outerValue%, 0, 0
ImageSearch, imageX, imageY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *25 %outerValue%
if (ErrorLevel = 0) {
; Iterate through the second array
for innerIndex, innerValue in secondArray {
; innerIndex = Index of the current element; 1, 2, etc...
; innerValue = The value of the string at that index
Tooltip, Searching for %innerValue%, 0, 0
ImageSearch, imageX, imageY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *25 %innerValue%
if (ErrorLevel = 0) {
MouseClick, Left, %imageX%, %imageY%
Sleep, 1000
}
}
}
}
}