Is there a way to detect and get local coordinates of mouse position(location) in SwiftUI

Is there a way to detect and get local coordinates of mouse position moving over some View
for macOS or multi platform apps ?

My understanding is no. You would have to setup tracking rectangles on the NSWindow and manually update them, while using your own NSView subclass to receive mouse over callbacks, that you then insert into SwiftUI.

This solution would be for macOS only.

I dont think iOS has any cursor support. The accessibility feature people talk about just emulates a virtual touch, its not an actual cursor.

People on the Apple developer forums, including Apple SwiftUI engineers might know more about specific SwiftUI capabilities on macOS

SwiftUI doesn’t appear to provide anything like this - the closest I could find was the onHover(perform:) modifier, which is only as granular as “is the mouse over this view or not?”

This article may be helpful, however - the author uses NSViewRepresentable to create a View that emits the cursor location as the user moves it.

1 Like

Great! It works and looks not like scary hack :slightly_smiling_face:
Thank you

1 Like
Terms of Service

Privacy Policy

Cookie Policy