4

Disclaimer; barely a novice when it comes to Swift/macOS development.


The Ctrl+Shift+4 screenshot tool on macOS, has an accompanying cursor change that I've been looking for a way to replicate.

In its initial launch state, it's a crosshair with pixel coordinates, and switching it to window capture, by pressing space, changes the cursor to a camera.

No matter if you tab to other applications, the cursor remains changed, globally.

I've tried to replicate this behaviour by creating an see-through fullscreen NSWindow with a .floating level and changing the cursor for the content view.

This works, sort of... But has some annoying quirks.

Firstly it seems the opacity of the view cannot be a strict zero, but at least some fraction >= 0.01. I found no mention of this in the docs of NSView, but it's mentioned for UIView here. For the built-in screenshot tool, there doesn't seem to be any 0.01 alpha overlay.

Secondly, tabbing to any application, the .floating window is still on top, but makes the view loose focus, which means the cursor turns back to default. In this case, the built-in screenshot tool has a very short delay, when a new application is fronted, and then changes the cursor to the custom one again.

Am I taking the completely completely wrong approach, or do I just need to twist some nobs and dials, for this one to work?

Any pointers much appreciated!

rhardih
  • 863
  • 1
  • 10
  • 18
  • I'm not sure this is going to be possible. The window server can simply do things that user-land applications can't. In principle, each application controls the shape of the cursor *while that application is active*. Imagine the chaos that would ensue if any application could change the cursor for your application. So no, I don't think a user-land application can force the cursor to change globally. – James Bucanek Apr 23 '19 at 18:11
  • I don't think this will help you—but who knows: You don't have to set the `alpha` of view/window to nearly 0.0. Just make the background of the window and the content of the view transparent (`NSColor.clearColor`). Remember to set/return `NO` from `isOpaque`. – James Bucanek Apr 23 '19 at 19:28
  • Mareks-MacBook-Pro-2:Downloads xhruso00$ whereis screencapture /usr/sbin/screencapture => Use Hopper (trail) to reveal code. If you can get hands on 10.10 version it is very easy to read Objective-C code dissasembly than Swift code dissasembly https://imgur.com/a/yfyFFyS – Marek H Apr 24 '19 at 10:31
  • @JamesBucanek I can understand why something like that might be limited, I just haven't come across any documentation, that it's explicitly not possible. Perhaps I need to look into accessibility APIs and extended capabilities. – rhardih Apr 25 '19 at 13:05
  • [This dirty hack](https://stackoverflow.com/a/32195746) shall help you keep the cursor shape despite your window losing focus. – hidefromkgb Apr 26 '19 at 18:56

0 Answers0