Page 1 of 1
[V2] [Function] Select screen region with mouse
Posted: 20 Mar 2023, 05:10
by SpeedMaster
SelectScreenRegion(Key, Color, transparency)
The Function: (by FanaticGuru)
Code: Select all
SelectScreenRegion(Key, Color := "Lime", Transparent:= 80)
{
CoordMode("Mouse", "Screen")
MouseGetPos(&sX, &sY)
ssrGui := Gui("+AlwaysOnTop -caption +Border +ToolWindow +LastFound -DPIScale")
WinSetTransparent(Transparent)
ssrGui.BackColor := Color
Loop
{
Sleep 10
MouseGetPos(&eX, &eY)
W := Abs(sX - eX), H := Abs(sY - eY)
X := Min(sX, eX), Y := Min(sY, eY)
ssrGui.Show("x" X " y" Y " w" W " h" H)
} Until !GetKeyState(Key, "p")
ssrGui.Destroy()
Return { X: X, Y: Y, W: W, H: H, X2: X + W, Y2: Y + H }
}
Example :
Press:
Ctrl +
Win +
Mouse or Press:
L+
Mouse to select screen region.
Code: Select all
#Requires AutoHotkey v2
; Select Screen Region with Mouse
^#LButton:: ; Control+Win+Left Mouse to select with Red
{
Area := SelectScreenRegion("LButton", "Red", 30)
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
{
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
}
Esc:: ExitApp
SelectScreenRegion(Key, Color := "Lime", Transparent:= 80)
{
CoordMode("Mouse", "Screen")
MouseGetPos(&sX, &sY)
ssrGui := Gui("+AlwaysOnTop -caption +Border +ToolWindow +LastFound -DPIScale")
WinSetTransparent(Transparent)
ssrGui.BackColor := Color
Loop
{
Sleep 10
MouseGetPos(&eX, &eY)
W := Abs(sX - eX), H := Abs(sY - eY)
X := Min(sX, eX), Y := Min(sY, eY)
ssrGui.Show("x" X " y" Y " w" W " h" H)
} Until !GetKeyState(Key, "p")
ssrGui.Destroy()
Return { X: X, Y: Y, W: W, H: H, X2: X + W, Y2: Y + H }
}
Example without function:
Converted from V1 to V2 by Speedmaster
Regards
Re: [V2] Selcet screen région and save it to clipboard
Posted: 20 Mar 2023, 23:33
by FanaticGuru
SpeedMaster wrote: ↑20 Mar 2023, 05:10
Selcet screen région and save it to clipboard (converted from V1)
Need to add
-DPIScale to the Gui to have it work for screens at other than 100%.
I have seen this done several different ways and this using of a Gui seems the simplest.
Now you know the next thing people are going to ask for is for you to convert the entire Screen Clipper to v2.
FG
Re: [V2] Selcet screen région and save it to clipboard
Posted: 21 Mar 2023, 03:05
by SpeedMaster
Thanks for the feedback and suggestion.
FanaticGuru wrote: ↑20 Mar 2023, 23:33
Now you know the next thing people are going to ask for is for you to convert the entire Screen Clipper to v2
Not for me... I'm still learning the new syntax.
Re: [V2] Selcet screen région and save it to clipboard
Posted: 26 Mar 2023, 10:22
by NPerovic
Microsoft: "Win+Shift+s"
Re: [V2] Selcet screen région and save it to clipboard
Posted: 28 Mar 2023, 07:17
by elbitjusticiero
NPerovic wrote: ↑26 Mar 2023, 10:22
Microsoft: "Win+Shift+s"
This is not for taking a screenshot. It's for retrieving the coordinates of a screen region, marking it with the mouse.
Re: [V2] Selcet screen région and save it to clipboard
Posted: 28 Mar 2023, 12:11
by FanaticGuru
Here is a simplified version as a function:
Code: Select all
#Requires AutoHotkey v2
; Select Screen Region with Mouse
^#LButton:: ; Control+Win+Left Mouse to select with Red
{
Area := SelectScreenRegion("LButton", "Red", 30)
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
{
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
}
Esc:: ExitApp
SelectScreenRegion(Key, Color := "Lime", Transparent:= 80)
{
CoordMode("Mouse", "Screen")
MouseGetPos(&sX, &sY)
ssrGui := Gui("+AlwaysOnTop -caption +Border +ToolWindow +LastFound -DPIScale")
WinSetTransparent(Transparent)
ssrGui.BackColor := Color
Loop
{
Sleep 10
MouseGetPos(&eX, &eY)
W := Abs(sX - eX), H := Abs(sY - eY)
X := Min(sX, eX), Y := Min(sY, eY)
ssrGui.Show("x" X " y" Y " w" W " h" H)
} Until !GetKeyState(Key, "p")
ssrGui.Destroy()
Return { X: X, Y: Y, W: W, H: H, X2: X + W, Y2: Y + H }
}
At some point I would like to do a complete rewrite of Screen Clipper to get familiar with the v2 Gdip. This is my start.
FG
Re: [V2] Selcet screen région and save it to clipboard
Posted: 28 Mar 2023, 18:31
by SpeedMaster
FanaticGuru wrote: ↑28 Mar 2023, 12:11
Here is a simplified version as a function:
At some point I would like to do a complete rewrite of Screen Clipper to get familiar with the v2 Gdip. This is my start.
FG
It's amazing.
Thanks for sharing.
I updated my previous post with your function...
viewtopic.php?p=513351#p513351
Cheers
Re: [V2] [Function] Select screen region with mouse
Posted: 15 Jun 2023, 01:16
by Spitzi
Thank you @FanaticGuru and @SpeedMaster .
Very efficient way to select a screen area. Will implement it into my script. Thank you both!!
Re: [V2] [Function] Select screen region with mouse
Posted: 15 Jun 2023, 02:54
by Spitzi
Hi again. I adapted the function for my needs, maybe it comes in handy for somebody else:
Variations:
- function can be called and waits until user begins selecting the screen - Tooltip is displayed
- escape will quit the screen selection process
- option to add a second transparent gui under the mouse cursor. The application that I use for work changes the cursor position itself (to stay within a part of the screen) - while selecting the screen region, I want to protect the cursor from that - that's what the transparent gui is for
- option to restrict the selection to a certain screen area
Code: Select all
#Requires AutoHotkey v2
; Select Screen Region with Mouse
R:: ; R to select with Red
{
Area := SelectScreenRegion("LButton", "Red", 30)
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
{
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
}
Q:: ExitApp
; Idea 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
; - cpSize: size of a transparent gui to drag along, that protects the cursor from the underlying app (usefull when selecting something in PACS)
; - subA: subarea, the selection process is restricted to (Map with x,y,w,h)
; 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", cpSize:=0, subA:=false)
{
CoordMode("Mouse", "Screen")
Loop {
Sleep(20)
Tooltip(TTinfo)
if GetKeyState("Esc", "p") { ; cancel if escape was pressed
Tooltip()
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
if (cpSize > 0) { ; create cursor protection gui if needed
cpGui := Gui("+AlwaysOnTop -caption +Border +ToolWindow +LastFound -DPIScale", "CursorProtection")
cpGui.Show("w" cpSize*2 " h" cpSize*2)
WinSetTransparent(1, cpGui.Hwnd)
}
CoordMode("Mouse", "Screen")
MouseGetPos(&sX, &sY)
ssrGui := Gui("+AlwaysOnTop -caption +Border +ToolWindow +LastFound -DPIScale")
WinSetTransparent(Transparent)
ssrGui.BackColor := Color
Loop
{
Sleep 20
MouseGetPos(&eX, &eY)
if (cpSize > 0) {
WinMove(eX-cpSize,eY-cpSize,cpSize*2,cpSize*2,cpGui.Hwnd) ; moves the cursor protection gui
}
W := Abs(sX - eX), H := Abs(sY - eY)
X := Min(sX, eX), Y := Min(sY, eY)
ssrGui.Show("x" X " y" Y " w" W " h" H)
} Until !GetKeyState(Key, "p")
ssrGui.Destroy()
try cpGui.Destroy()
Return { X: X, Y: Y, W: W, H: H, X2: X + W, Y2: Y + H }
}
Greets Spitzi
Re: [V2] [Function] Select screen region with mouse
Posted: 01 Jul 2023, 05:33
by aliztori
@Spitzi
It would be good if we could lock the the Lbutton, so that it does not actually click on the place we want to select.
Re: [V2] [Function] Select screen region with mouse
Posted: 01 Jul 2023, 15:19
by Spitzi
@aliztori, you are right, and we can.... I created a new version of the skript, shorter, and without the transparent gui that was used to take the LButton-Event...
Using #HotIf to intercept LButton if the selection window exists...
Is that what you are looking for?
Code: Select all
#Requires AutoHotkey v2
; 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
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
{
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, L to select
{
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
}
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
}
#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")
; 1) create GUI
ssrGui := Gui("+AlwaysOnTop -caption +Border +ToolWindow +LastFound -DPIScale", "SSR-GUI")
WinSetTransparent(Transparent)
ssrGui.BackColor := Color
ssrGui.Show("x1 y1 w1 h1")
; 2) show tooltip until selection begins
Loop {
Sleep(30)
Tooltip(TTinfo)
if GetKeyState("Esc", "p") {
Tooltip()
return
}
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
; 3) show gui where mouse is
CoordMode("Mouse", "Screen")
MouseGetPos(&sX, &sY)
Loop
{
Sleep 30
MouseGetPos(&eX, &eY)
W := Abs(sX - eX), H := Abs(sY - eY)
X := Min(sX, eX), Y := Min(sY, eY)
ssrGui.Show("x" X " y" Y " w" W " h" H)
} Until !GetKeyState(Key, "p")
; 4) clean up
ssrGui.Destroy()
Return { X: X, Y: Y, W: W, H: H, X2: X + W, Y2: Y + H }
}
Re: [V2] [Function] Select screen region with mouse
Posted: 10 Aug 2023, 06:56
by elbitjusticiero
Pressing Escape while the message box is showing gives me an error (This value of type "String" has no property named "X") but still disables mouse clicks systemwide.
Re: [V2] [Function] Select screen region with mouse
Posted: 10 Aug 2023, 16:57
by Spitzi
@elbitjusticiero , 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 }
}