Capturing Computer Screen Measurements (An AutoHotkey Tool)

Calibrate the MouseMeasure AutoHotkey Tool to Grab Calculated Lengths from Your Computer Monitor

Recently, a reader asked, “Do you think it is conceivable to create a screen ruler in AHK that can be calibrated to my native application ruler. The problem I have now is that I take tons of measurements off the screen and then I have to type that number back into a document. I would love to make a ruler that can basically calibrate with the native app ruler once and make the data from the AHK ruler transfer automatically to the clipboard or better yet straight to the document.”

I responded, “The application you’re looking at is quite conceivable. You can pick coordinates off the screen with the MouseGetPos command and save them. Then you can possibly use two clicks to calculate the difference between the two in pixels then convert it to your scale. There are a number of methods for sending data to documents. It is certainly within the realm of possibilities.”

I then searched the AutoHotkey board only to find that he had already posted the same Ruler question in the “Ask for Help” forum. Fortunately, AutoHotkey Forum user colt had already posted a response. With the heart of the work completed by colt, I decided to add an onscreen calibration method.

Pythagorean Theorem

Pythagoras gave every high school math student a reason to remember his name. He provided the method for calculating the hypotenuse of a right triangle. For most people, the formula fell into the toolbox of things-I’ll-never-do-again. But for anyone who wants to measure distances on a computer screen, the Pythagorean Theorem returns with a vengeance.

Measuring Spans on a Computer Screen

Computer screens use pixels in a matrix of rows and columns to create text and images. Each pinpoint can vary in color and pixel density (dots per inch) depends upon the monitor’s resolution, but the dot matrix sits stationary on the screen. Capturing the number of pixels in a row or column does not present much of a challenge. However, the length of a hypotenuse (`C` in the image) requires a special calculation:

While difficult to work out with pencil and paper, an AutoHotkey calculation can quickly return the distance in pixels between two sets of screen coordinates—(sx,sy) and (x,y):

``````MouseGetPos,x,y
dx := x-sx
dy := sy-y
dist := round(  ((dx)**2 + (dy)**2) **.5  ,3)``````

This snippet of code acts as the core for the script I call MouseMeasure.ahk. I modified the original code to allow for selectable ranges and units. Next, I added a new Hotkey combination for capturing a pixel scale from the computer screen—popping open a calibration GUI window for inputting the length of the calibration scale and units (e.g. miles, meters, inches,…).

I changed the previous measurement Hotkey to Shift+LButton and implemented an auto-calibration Hotkey Ctrl+LButton. Both Hotkeys use the same position-grabbing code—the user simultaneously holds down the modifying key and the left mouse button while dragging the mouse across the computer screen. I tested the new code on a map of Rhode Island.

Once calibrated to the onscreen scale, the measurement tool (Shift+LButton) captures the calculated distance between any two points (as a crow flies) on the computer screen.

Note: The Shift+LButton subroutine also saves the measured distance in the Windows Clipboard for pasting into other documents.

To manually open and set calibration, use the Ctrl+Alt+Z Hotkey combination. I added a MsgBox window with instructions that pops up when the script first loads. (Comment out the MsgBox line with a semi-colon if you find the Help window annoying.)

If while calibrating or measuring onscreen distances, you find your mouse a little wobbly, you may want to consider Hotkey techniques found in the MousePrecise.ahk script. Toggling a single key in the numeric keypad holds down the left mouse button while using the number keys to incrementally move the cursor pixel-by-pixel. You can find a discussion of MousePrecise in the book AutoHotkey Hotkey Tips, Tricks, Techniques, and Best Practices.

The final script:

```Pixels := 96
NumUnits := 1
Units := Inches

Gui, +ToolWindow
Gui, Add, Text, , Use Control+LeftMouseClick, Hold, and Drag`rfor Auto-Calibrate
Gui, Add, Edit, vPixels w75 Section, %Pixels%
Gui, Add, Text, ys , Pixel Range
Gui, Add, Edit, vNumUnits w75 xm section, 1
Gui, Add, Text, ys , Calibration range
Gui, Add, Edit, VUnits w75 xm section, Inches
Gui, Add, Text, ys , Calibration Units
Gui, Add, Button, gCalibrate ys ,Submit

scaleFactor := NumUnits/Pixels ; .568 ;your calibration factor to convert pixels to units

Help =
(
To Calibrate Range:

1. Cursor at start point — CTRL+LButton and hold.
2. Drag to end of scale and release.
3. Enter length of scale and units i.e. 10 miles.

Manual Calibration: CTRL+ALT+Z

To Measure Distance:

1. Cursor at starting point — SHIFT+LButton and hold.
2. Drag to end of measurement and release.
3. Distance displayed and saved to Clipboard.

CTRL+ALT+F12 for this Help Message.
)

HelpMe:
MsgBox, 1, MouseMeasure Help, % Help
Return

^!F12::GoSub, HelpMe

Ctrl & LButton::
Shift & LButton:: ;hold down shift and left mouse button to start measuring
mouseGetPos,sx,sy ;start position to measure from
setTimer updatePos,50
return

Shift & LButton Up:: ;release to capture measurement
setTimer updatePos,off
tooltip distance measured is : %distCalibrated% %units%
clipboard := distCalibrated
Sleep, 10000
ToolTip
return

Ctrl & LButton Up::
setTimer updatePos,off
ToolTip
GuiControl, , Pixels, %dist%
ManSet:
Gui, Show, , Scale Calibration
Return

^!Z::GoSub, ManSet

updatePos:
mouseGetPos,x,y
dx := x-sx
dy := sy-y
dist := round(  ((dx)**2 + (dy)**2) **.5  ,3)
distCalibrated := round(dist * scaleFactor,3)
tooltip [%dx%:%dy%]`n%dist% px`n%distCalibrated% %units%
return

Calibrate:
Gui, Submit
scaleFactor := NumUnits/Pixels
Return
```

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)

This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)

Find my AutoHotkey books at ComputorEdge E-Books!

Find quick-start AutoHotkey classes at “Robotic Desktop Automation with AutoHotkey“!

9 thoughts on “Capturing Computer Screen Measurements (An AutoHotkey Tool)”

1. […] the MouseMeasure.ahk script (discussed last time), I added a pop-up window displaying instructions on how to use the app. I formatted the text with […]

Like

2. […] the blog “Capturing Computer Screen Measurements (An AutoHotkey Tool)“, I added a calibration method to an on-screen ruler for extracting distances from any image. […]

Like

3. […] I could probably combine the UpdatePos subroutine (from “Capturing Computer Screen Measurements“) and DrawStuff subroutine but I want to wait until I add other features. I may need to […]

Like

4. Temoor says:

Hi Jack,
Where did you post your final code to the ruler. I checked the AHK forum and your blog. Your blog final script seems to be the original one?

Like

• Temoor Anwar MD says:

Great found it. I should have tried there first. Definitely, getting you a latte for that. The quicker calibration is awesome. Any chance you can help me add to the calibration menu something that captures amount of measurements. For example like X x Y x Z. So if I want to just record 1 or 3 measurements to be copied to clip board. I tried your previous script that had 3 but not sure if it had to do with the compass call. Also the marks are sustaining and only appearing on dominant screen. An we try mutlimonitor? The always on top didn’t work. Anyways to make them disappear after a click or time. More coffee to come and thanks! Your saving years of life.

Like

• Use the Delete key to remove the line and ToolTip. Use Ctrl+Escape to exit.

Like

5. Great addition in using GDIP.

Any chance you can make the ruler work on multi-monitors?
It seems like the drawn graphics works on the main monitor and doesn’t disappear.
Im assuming the angles are lost since that was a compass function?
Any chance you can modify of guide me to where in the script like last time to obtain 3 measurements or menu option in the beginning to tabulate 1, 2, or 3 measurements before copying to a clipboard.

Thank you Jack, this is awesome!

Like

• I haven’t worked with multi-monitors.

I didn’t need angles for my scripts but you should be able to get the code from the Compass script.

I’m planning to add multi-legs, but it will take a couple of weeks.

Like