Reputation: 151
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.
Upvotes: 5
Views: 2709
Reputation: 439
Swift 5:
let mousePosition = CGPoint(x: NSEvent.mouseLocation.x, y: (NSScreen.main?.frame.size.height)! - NSEvent.mouseLocation.y)
Upvotes: 0
Reputation: 1946
Get right position code:
CGPoint mousePoint = CGPointMake([NSEvent mouseLocation].x, [NSScreen mainScreen].frame.size.height - [NSEvent mouseLocation].y);
Upvotes: 2
Reputation: 2991
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.
Upvotes: 6
Reputation: 2200
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.
Upvotes: 2