, you are right, there were a few issues with the code... this should work better:
Code: Select all
#Requires AutoHotkey v2
; examples how to use
; Select Screen Region with left button
R:: ; R to select with Red inside a predefined area on screen
{
allowSubarea := { X: 100, Y: 100, W: 200, H: 200 }
; allowSubarea := false
if Area := SelectScreenRegion("LButton", "Red", 30, "Please select something inside the area", allowSubarea )
MsgBox "Top Left: X " Area.X "`tY " Area.Y "`nBottom Right X " Area.X2 "`tY " Area.Y2 "`n`nWidth " Area.W "`tHeight " Area.H
}
L:: ; L to select with defaults, instantly beginning when L is pressed down
{
if Area := SelectScreenRegion("L")
MsgBox "Top Left: X " Area.X "`tY " Area.Y "`nBottom Right X " Area.X2 "`tY " Area.Y2 "`n`nWidth " Area.W "`tHeight " Area.H
}
T:: ; T to start, W to select
{
if Area := SelectScreenRegion("W", , , "Press W to select")
MsgBox "Top Left: X " Area.X "`tY " Area.Y "`nBottom Right X " Area.X2 "`tY " Area.Y2 "`n`nWidth " Area.W "`tHeight " Area.H
}
X:: ; X to start, LButton to select
{
if Area := SelectScreenRegion("LButton", "Red", 30, "Please select something with the left mouse Button")
MsgBox "Top Left: X " Area.X "`tY " Area.Y "`nBottom Right X " Area.X2 "`tY " Area.Y2 "`n`nWidth " Area.W "`tHeight " Area.H
}
Q:: ExitApp
; this must be the top Hotkey using LButton in the skript , for skript to always work
#HotIf WinExist("SSR-GUI")
LButton:: { ; intercepts LButton click, does nothing exept showing a warning
ToolTip("Left mouse button`nignored by script", , , 2)
Sleep(100)
ToolTip( , , , 2)
}
#HotIf
; initially by FanaticGuru https://www.autohotkey.com/boards/viewtopic.php?f=83&t=115226
; Key can be LButton, or M or whatever, e.g: Area := SelectScreenRegion("LButton", "Lime", 30)
; Lets the user select a portion of the screen
; - key: the key to be held while selecting the screen
; - color: the color the selection rectangle will have
; - transaprent: 0 is invisible, 255 is fully visible
; - TTinfo: Tooltip to show before selection begins
; - subA: subarea, the selection process is restricted to (Map with x,y,w,h), false to turn that feature off
; returns the selected area as a map, or false if escaped
SelectScreenRegion(Key, Color := "Lime", Transparent:= 80, TTinfo:="Please select screen area. Hit Esc to chancel", subA:=false) {
local X:=0, Y:=0, W:=0, H:=0, X2:=0, Y2:=0 ; set return variables to an initial value
CoordMode("Mouse", "Screen")
; 0) create Gui first to intercept left mouse button
ssrGui := Gui("+AlwaysOnTop -caption +Border +ToolWindow +LastFound -DPIScale", "SSR-GUI")
WinSetTransparent(Transparent)
ssrGui.BackColor := Color
ssrGui.Show("x1 y1 w1 h1")
; 1) show tooltip until selection begins
Loop {
Sleep(30)
Tooltip(TTinfo)
if GetKeyState("Esc", "p") {
Tooltip()
ssrGui.Destroy()
return false
}
MouseGetPos(&mX, &mY)
if subA { ; restrict to subarea, if one is given
new_mX := (mX < subA.X) ? subA.X : mX ; new coordinates of mouse in subarea
new_mX := (mX > subA.X+subA.W) ? subA.X+subA.W : new_mX
new_mY := (mY < subA.Y) ? subA.Y : mY
new_mY := (mY > subA.Y+subA.H) ? subA.Y+subA.H : new_mY
if !((mX == new_mX) && (mY == new_mY)) ; set the new coordinates if they are not the same
MouseMove(new_mX, new_mY)
}
} Until GetKeyState(Key, "p")
ToolTip()
; 2) show gui where mouse is
MouseGetPos(&sX, &sY)
Loop
{
if GetKeyState("Esc", "p") {
Tooltip()
ssrGui.Destroy()
return false
}
Sleep(30)
MouseGetPos(&eX, &eY)
W := Abs(sX - eX), H := Abs(sY - eY)
X := Min(sX, eX), Y := Min(sY, eY)
ssrGui.Move(X, Y, W, H)
} Until !GetKeyState(Key, "p")
; 3) clean up
ssrGui.Destroy()
return { X: X, Y: Y, W: W, H: H, X2: X + W, Y2: Y + H }
}