How can i get mouse click event element in cocoa

hugo411

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.

Ken Thomases

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;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I prevent triggering an on click event when the actual mouse click didn't start on the target element?

How can I get selected text on a element click event?

How can i get a mouse move event?

How can I handle the Angular (click) event for the middle mouse button?

How can I call the mouse right click event in SwingGui?

How I can get a click event

How do I get the coordinates of a mouse click on a canvas element?

How can I get a global mouse event in GTK?

How i can emit event from directive by click outside of element?

How can I bind click event in repeater element in winjs?

How can I add an on click event on a div with multiple element inside

How can I distinguish wheel button click event from mouse press event?

get mouse event in cocoa window controller

How can I detect if mouse is down outside of window in cocoa?

How can I get the click event inside an innerHTML?

Highcharts - How can I get the nearest point on click event

How can I get on click event on PreferenceCategory's tilte?

Can I get the mouse position in OpenCV without a mouse event?

How can I delete a record (that is selected by mouse) from a datasheet using a Delete button On Click Event?

How can I move mouse cursor and click?

How i can send mouse click in powershell

How can I focus a JLabel with a mouse click?

How can I get an output by click on a select2 element?

How can I get the element that is clicked on with a click handler added in a forEach?

How can I trigger the click event of another element in ng-click using angularjs?

how to get click event of li element in jquery?

how to get click event of row element of jstree?

How do I get DOM element by mouse click on PyQT5 QWebView

How can I get in a "mouseover" event over a dataview table, in Wicket, the Row I'm over it with my mouse?