Hypherion
Hypherion

Reputation: 185

How to track the location of a window belonging to another app

When screen sharing a specific window on macOS with Zoom or Skype/Teams, they draw a red or green highlight border around that window (which belongs to a different application) to indicate it is being shared. The border is following the target window in real time, with resizing, z-order changes etc.

See example:

enter image description here

What macOS APIs and techniques might be used to achieve this effect?

Upvotes: 4

Views: 291

Answers (1)

Nick Moore
Nick Moore

Reputation: 15857

You can find the location of windows using CGWindowListCopyWindowInfo and related API, which is available to Sandboxed apps.

This is a very fast and efficient API, fast enough to be polled. The SonOfGrab sample code is great platform to try out this stuff.

You can also install a global event tap using +[NSEvent addGlobalMonitorForEventsMatchingMask:handler:] (available in sandbox) to track mouse down, drag and mouse up events and then you can respond immediately whenever the user starts or releases a drag. This way your response will be snappy.

(Drawing a border would be done by creating your own transparent window, slightly larger than, and at the same window layer as, the window you are tracking. And then simply draw a pretty green box into it. I'm not exactly sure about setting the z-order. The details of this part would be best as a separate question.)

Upvotes: 4

Related Questions