使用 [NSEvent mouseLocation] 的位置错误
我为 Mac 制作了一个 iPhone 远程鼠标控制器应用程序:iPhone 应用程序将坐标值发送到 Mac,然后 Mac 处理鼠标位置值。
为了获取 Mac 上的当前鼠标位置,接收者调用 [NSEvent mouseLocation]。
x 的值始终正确,但 y 的值错误。
我使用了“while”循环来处理这个事件。
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
........
每个循环周期的y值不同。例如,第一次循环时y值为400,下一次循环时y值为500;然后 y 在下一个循环中再次变为 400。
鼠标指针不断上下移动,两个不同的y值之和始终为900。(我认为是因为屏幕分辨率是1440 * 900。)
我不知道为什么会发生这种情况,该怎么办,以及如何解决调试它。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一种获得正确 Y 值的方法:
基本上,我已经获取了屏幕高度:
然后我获取屏幕高度并从中减去 mouseLocation 的 Y 坐标,因为 mouseLocation 从底部/左侧返回坐标,这将为您提供从顶部开始的 Y 坐标。
这在我的控制鼠标位置的应用程序中起作用。
Here is a way you can get the proper Y value:
Basically, I've grabbed the screen 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.
This is working in my app that controlling the mouse position.
我不知道为什么它会在没有看到更多代码的情况下发生变化,但很可能它与
mouseLoc = [NSEvent mouseLocation];
返回一个点有关,该点的原点位于屏幕的左下角,而不是通常的左上角。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.获取正确的位置代码:
Get right position code:
斯威夫特5:
Swift 5: