Blog

How a Screenshot Tool Finds the Window or Button Under Your Cursor

Written by InSnap · Published · screenshot, auto detect window, accessibility, how it works

How a Screenshot Tool Finds the Window or Button Under Your Cursor

Hover and the right box appears, scroll and it grows a level. How InSnap combines pixel edges, the system window list and accessibility data to find windows and buttons.

In InSnap you do not have to drag a box around a window or a button. You move the cursor, a box snaps to whatever is under it, and the mouse wheel grows that box one level at a time. Behind that are three different sources of information, each with its own strengths, merged into one list of nested boxes.

Source 1: pixel edges

The first source needs nothing from the operating system, so it always works, even on a game or a custom-drawn interface. It looks only at the frozen screenshot.

  1. Shrink the screen to a brightness grid, at most 900 cells on the long side, built once per capture.
  2. Mark where edges are, and store running totals so that "how much of this line is an edge?" can be answered instantly for any position and length.
  3. Test for an edge across two cells, not one. Real window borders are often soft: a translucent taskbar can differ from the page by only about 20 grey levels, and a single-cell jump gets averaged away when the picture is shrunk. Comparing across two cells keeps those edges.
  4. Grow outward from the cursor in four directions until an edge stops each side. That gives one box. Growing again from just outside it gives the next, and so on.
  5. Keep only the meaningful levels: those that are clearly bigger, or that newly touch two or more screen edges, which is typical of a window or a content area.

Building the grid for a whole screen takes milliseconds in native code, so it is done directly with no background thread.

SNAPPING A SELECTION TO A WINDOW

Source 2: the system's window list

The operating system knows exactly where each window is. Using that list gives precise edges instead of pixel guesses, and it needs no extra permission. The window under the cursor always becomes one of the levels, and any pixel level that sticks out beyond it is discarded.

Source 3: the interface elements inside a window

Buttons, text fields and groups come from the accessibility interface. On Mac this needs the Accessibility permission; on Windows it uses UI Automation, which needs no prompt.

Two details are worth knowing:

  • It is asked in the background. The query goes to the other application, which may be slow, so it runs on a worker thread with a short time limit. The box first appears from the other sources and refines once the answer arrives.
  • Some apps hide their contents. Browsers and Electron apps do not build the accessibility tree of a web page until something asks for it. InSnap asks once per app so that page elements can be found.

On Windows the lookup also has to avoid a trap: the screenshot overlay sits on top of everything, so asking "what is at this point?" would only ever find the overlay. InSnap first finds the other app's window at that point in the window list, then searches inside it for the innermost element that contains the point.

How the three are merged

The result is one chain of boxes from innermost to outermost:

  • If elements are available and the innermost one is not nearly the whole window, only the element chain is used, so a button stays a button and is not stolen by the edge of an icon inside it.
  • If there are no elements, or the app only exposes the window itself (games, custom-drawn interfaces), pixel levels inside the window are added.
  • The real window is always one level.
  • The last level is the whole screen, so scrolling outward always ends somewhere.
  • Levels whose edges are within 1.5 points of each other are merged into one.

The wheel, pinch and Space

  • Scroll wheel moves one level outward or inward. A burst of wheel events from one physical notch is combined into a single step.
  • Trackpad scrolling and pinch change levels as you move, with about 12% of pinch per level.
  • Space jumps between the whole window and the innermost element.
Magnifier and pixel colours while selecting

Where it does not help

SituationWhat happens
Solid-colour area with no edgesNothing to detect, so you draw the box yourself
Game or custom-drawn interfaceWindow plus pixel levels, no individual buttons
Accessibility permission missing on MacWindows and pixel levels only
Element changes during the query (a hover menu)The box may refine after a moment

FAQ

Do I need to grant a permission for automatic window detection?

Window detection works without one. Finding buttons and fields inside a window uses the accessibility interface, which asks for the Accessibility permission on a Mac.

Why does the box jump to a different size after a moment?

The first box comes from the quick sources. The accessibility answer comes from the other app and can arrive a moment later, at which point the box refines to the exact element.

Does detection send my screen anywhere?

No. The grid and the matching are computed on your computer from the frozen capture and from the window list.

Can I turn automatic detection off?

Yes. It is a setting in the screenshot options, and finding interface elements can be switched off separately from window detection.

Your next screenshot is one hotkey away.

macOS 14.4+ (Apple Silicon & Intel) · Windows 10+ (64-bit)