How to store (and use) the current mouse position?

时光毁灭记忆、已成空白 提交于 2019-12-13 16:15:51

问题


What is the best way to store the current mouse position (system-wide) and then (later) put the mouse at that stored point?

[NSEvent mouseLocation] gets me the position, and I can move the mouse with a CGEventMouseMoved, but they each use a different co-ordinates system (I believe y=0 is the top for NSEvent and the bottom for a CGEvent).

I'm worried about the robustness of capturing the screen height and using it to convert between the two - or is this the best approach?


回答1:


Yes, using the main screen's height is the standard way of flipping screen coordinates.




回答2:


You could use a CGEventTap to get the mouse events instead of NSEvent. Then you have your choice of flipped or unflipped at the time the event arrives.

(I believe y=0 is the top for NSEvent and the bottom for a CGEvent).

You have that the wrong way around. Quartz uses flipped, which is y=0=top. AppKit uses unflipped, which is y=0=bottom. (Edit: For events, that is. Quartz 2D, the drawing API, uses unflipped coordinates, the same as AppKit does.)



来源:https://stackoverflow.com/questions/2126622/how-to-store-and-use-the-current-mouse-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!