Wrong location using [NSEvent mouseLocation]
Asked Answered
L

4

5

I make a iphone remote mouse controller application for Mac: the iPhone application sends the coordinate values to the Mac, which then processes mouse location value.

To get the current mouse location on the Mac, the receiver calls [NSEvent mouseLocation].

The value for x is always correct, but the value for y is wrong.

I used a "while" loop to process this event.

while (1) {
    mouseLoc = [NSEvent mouseLocation];

    while ((msgLength = recv(clientSocket, buffer, sizeof(buffer), 0)) != 0) {
          CGPoint temp;
          temp.x = mouseLoc.x;
          temp.y = mouseLoc.y; // wrong value
          ........

A y value is different at each loop period. For example, y value is 400 at first loop, y is 500 at next loop; then y is 400 again at next loop.

The mouse pointer is coming up and down continuously, and sum of two different y values is always 900. (I think because the screen resolution is 1440 * 900.)

I don't know why it happens, what to do, and how to debug it.

Lobotomy answered 20/11, 2011 at 14:15 Comment(4)
As far as I know, mouseLocation is not a static method.Citified
@Citified mouseLocation is a class method of NSEvent.Quasi
Oh, I thought it wasn't. Sorry!Citified
@Lobotomy did you ever figure this out? I am having this issue.Helenehelenka
H
6

Here is a way you can get the proper Y value:

while (1) {
mouseLoc = [NSEvent mouseLocation];
NSRect screenRect = [[NSScreen mainScreen] frame];
NSInteger height = screenRect.size.height;

while ((msgLength = recv(clientSocket, buffer, sizeof(buffer), 0)) != 0) {
      CGPoint temp;
      temp.x = mouseLoc.x;
      temp.y = height - mouseLoc.y; // wrong value
      ........

Basically, I've grabbed the screen height:

NSRect screenRect = [[NSScreen mainScreen] frame];
NSInteger height = screenRect.size.height;

Then I take the screen height and subtract the Y coordinate of mouseLocation from it because mouseLocation returns coordinates from the bottom/left this will give you the Y coordinate from the top.

temp.y = height - mouseLoc.y; // right value

This is working in my app that controlling the mouse position.

Helenehelenka answered 12/4, 2012 at 7:27 Comment(1)
@Nicholaz it should work no problem if both monitors are the same resolution - otherwise you might need some extra math/checking which monitor the mouse is on - you'd have to try it to find out.Helenehelenka
N
2

I don't know why it would be changing without seeing more of your code, but there is a good possibility it has something to do with the fact that mouseLoc = [NSEvent mouseLocation]; returns a point whose origin is at the bottom left of the screen, instead of the top left where it would normally be.

Norval answered 15/12, 2011 at 23:35 Comment(0)
T
2

Get right position code:

CGPoint mousePoint = CGPointMake([NSEvent mouseLocation].x, [NSScreen mainScreen].frame.size.height - [NSEvent mouseLocation].y);
Twospot answered 25/1, 2013 at 18:36 Comment(0)
B
0

Swift 5:

let mousePosition = CGPoint(x: NSEvent.mouseLocation.x, y: (NSScreen.main?.frame.size.height)! - NSEvent.mouseLocation.y)
Brownlee answered 28/7, 2021 at 18:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.