Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouse coordinates in Objective-C?

What is the difference between these three methods of getting mouse coordinates:

  • [NSWindow convertBaseToScreen]
  • [NSEvent* locationInWindow]
  • [NSWindow mouseLocationOutsideOfEventStream]

I already checked the documentation, but the descriptions were too technical. Could someone explain the difference between these three in simpler terms?

like image 327
fdh Avatar asked Mar 16 '26 00:03

fdh


1 Answers

Every window and every view has its own coordinate system, which (unless you've modified it) starts at (0, 0) in the bottom-left and counts up and to the right. -[NSEvent locationInWindow] gives you the location where the event took place the window's coordinate system (that is, (0, 0) is the bottom-left of the window). -[NSWindow convertBaseToScreen:] takes a coordinate in the window's coordinate system and converts it to screen coordinates, so that (0, 0) now means the bottom-left of the whole screen.

-[NSWindow mouseLocationOutsideOfEventStream] gives you the location (in the coordinate system of the window you send it to) where the mouse is right now, unrelated to any event. This is rarely useful information.

like image 146
Chuck Avatar answered Mar 17 '26 15:03

Chuck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!