Reputation: 346
I receive events from clicks with a global monitor in macOS. The NSEvent give me coordinates but i need to get the UIElement.
Using the accessibility API, i can get the focused element but i need the clicked element which is totaly different. For example, if i click on a textarea, i get a AXTextArea element. If i click on the application taskbar, the focused element remains AXTextArea which make sense. I want the element associated to the mouse event.
I found on the web a method called accessibilityHitTest which is supposed to retrieve the UIElement under a given NSPoint. I always get null so i am not sure this is supposed to work that way.
Maybe there is another function which retrieve the UIElement from a given point in the screen?
Here is what ive tried :
NSEvent *event; // My event received from global monitoring
id clickedObject = [self accessibilityHitTest:event.locationInWindow];
//This give me an error because self is not a NSWindow.
NSEvent *event; // My event received from global monitoring
id clickedObject =
[[NSApp keyWindow] accessibilityHitTest:event.locationInWindow];
//This give me null at every click.
Maybe this is not possible but i just which i could get the UIElement under my mouse click and not the focused UIElement.
Upvotes: 0
Views: 625
Reputation: 90531
You need to use AXUIElementCopyElementAtPosition()
for this. Note that you have to be careful to provide the coordinates in the proper coordinate system. Since the event has no window
associated with (because it's for a different app), NSEvent
's locationInWindow
uses the Cocoa coordinate system with the origin in the lower-left of the primary display, with y
increasing in the up direction. AXUIElementCopyElementAtPosition()
takes coordinates in the Core Graphics coordinate system with the origin in the upper-left of the primary display, with y
increasing down. Basically, you do:
point.y = NSMaxY(NSScreen.screens[0].frame) - point.y;
Upvotes: 4