5

I want to be able to get the global caret position inside any application in Mac High Sierra using cocoa or appleScript. I already use NSEvent to get the keyboard and mouse hook but is there a way to get the caret position hook?

The caret is different from the mouse position. It moves on key event or mouse click. In windows, you can get the caret position almost anywhere. I want to know if there is the equivalent for macos.

I want to show a popup over the text caret, if i type on the keyboard or line return, it moves with the text.I tried getting the position of the key event, (locationInWindow) but it give me back the mouse position. I am not sandbox so i can even call applescripts

UPDATE : It is possible doing this by getting the bounds of the letter before the caret with the use of accessibility API.

thanks

hugo411
  • 320
  • 1
  • 12
  • 29
  • Possible duplicate of [Getting "global" mouse position in Mac OS X](https://stackoverflow.com/questions/2262516/getting-global-mouse-position-in-mac-os-x) – koen May 07 '18 at 15:48
  • 1
    You don't know what is a caret do you. – hugo411 May 07 '18 at 16:46
  • My bad, I misread your question. And yes, of course I know what a caret is. – koen May 07 '18 at 17:15
  • In fairness, on some (non-Mac) systems the insertion point moves with the mouse, and "caret" and "mouse pointer" are nearly synonymous. – Caleb May 07 '18 at 17:59
  • Yes, but I need to know when the caret is moving while typing (on line return for example). The caret doesn't move with the mouse, it stays on the letters and moves with keyboard or mouse click. – hugo411 May 08 '18 at 12:35
  • 1
    What is the context from which you're trying to find out? For example, keyboard input methods (e.g. Pinyin for entering Chinese text) can work with the app receiving the keystrokes to obtain the screen position of the caret. Are you developing something that could be an input method? – Ken Thomases May 10 '18 at 02:51
  • Also, possibly a duplicate of [How to get global screen coordinates of currently selected text via Accessibility APIs](https://stackoverflow.com/questions/6544311/how-to-get-global-screen-coordinates-of-currently-selected-text-via-accessibilit). – Ken Thomases May 10 '18 at 03:17
  • I want to show a popup over the text caret, if i type on the keyboard or line return, it moves with the text.I tried getting the position of the key event, (locationInWindow) but it give me back the mouse position. I am not sandbox so i can even call applescripts. – hugo411 May 10 '18 at 12:49
  • @KenThomases Yes i have looked at this post and this is good but when no text is selected it returns 0. I will test it again. – hugo411 May 10 '18 at 12:54
  • https://stackoverflow.com/questions/19315250/how-to-find-absolute-value-of-caret-position-in-pixels-using-cocoa-in-macos – TheNextman May 10 '18 at 21:04
  • @hugo411, did you find any solution? – Krishna Maru Dec 17 '18 at 13:55
  • 1
    Yes ill post solution soon sorry for the delay – hugo411 Jan 10 '19 at 18:26

1 Answers1

0

I don't have the opportunity to try it for myself just yet, so you might beat me to the punch of confirm/reject this.

UIEvent has addGlobalMonitorForEventsMatchingMask:handler: where the mask can have a value of NSEventMaskCursorUpdate and presumably the returned NSEvent object will contain a coordinate that can be acted upon (i.e. converted to screen-space).

Caveat here is the docs explicitly say

Key-related events may only be monitored if accessibility is enabled or if your application is trusted for accessibility access (see AXIsProcessTrusted).

Your post seems to suggest that you do not wish to use Accessibility API ("but if not using accessibility API") so that may mean you're out of luck in the specific combination of requirements you seek to fulfill.

christopherdrum
  • 1,513
  • 9
  • 25
  • Yes this is the first thing i tried, the NSEventMaskCursorUpdate gives the mouse position. I wish to use Accesibility API but if not possible applescript also. I am not sandboxed. Let me try again i have a doubt. – hugo411 May 10 '18 at 12:52
  • 1
    The NSEventMaskCursorUpdate is never triggered i just did the test and yes accessibility is enabled – hugo411 May 10 '18 at 13:15
  • Hmm, frustrating. I see in the docs for locationInWindow: `For non-mouse events the return value of this method is undefined.` I'm surprised the value doesn't correspond to the requested event. As for the triggering of the event, have you seen this thread? https://stackoverflow.com/questions/25496336/addglobalmonitorforeventsmatchingmask-not-working – christopherdrum May 10 '18 at 13:17
  • All my events works fine but NSEventMaskCursorUpdate don't do anything. It seems I need to add a trackingArea in order to work with this. – hugo411 May 10 '18 at 13:23
  • 2
    I have checked again on the web and the only solution is to get the range of the selected text of the letter before the caret. This will give the the coordinates of this letter. When I don't have any text (on new line), the code will generate a space, get the position and delete the space. – hugo411 May 10 '18 at 13:51