[英]Cocoa: how to prevent mouse move past coordinates?
Final goal is to prevent mouse move to another screen (dual display setup) unless a hotkey is held. 最终目标是防止鼠标移动到另一个屏幕(双显示设置),除非保持热键。
The best I came up with, is this: 我想出的最好的是:
[NSEvent addGlobalMonitorForEventsMatchingMask:NSMouseMovedMask handler:^(NSEvent *mouseMovedEvent) {
// does nothing yet
}]
The reason I went for global monitor is that my app does not have any windows (and views), it's a status bar app. 我去全球监视器的原因是我的应用程序没有任何窗口(和视图),它是一个状态栏应用程序。 So NSTrackingArea
went home. 所以NSTrackingArea
回家了。
Any help would be much appreciated. 任何帮助将非常感激。 In Java world, I would simply preventDefault()
the event object. 在Java世界中,我只是将preventDefault()
作为事件对象。 Now I need to get the same functionality in Objective-C. 现在我需要在Objective-C中获得相同的功能。 Ideally, I'd wish there would be "MouseMovedPastScreen" event, but apparently there's not. 理想情况下,我希望会有“MouseMovedPastScreen”事件,但显然没有。
Thanks. 谢谢。
Again, in Java, I would get the bounds of both screens, and stopped mouse at the corner positions. 再次,在Java中,我将获得两个屏幕的界限,并在角落位置停止鼠标。 And then would allow the event to bubble if the ⌘
key has been held during the event execution. 然后,如果在事件执行期间保持了⌘
键,则允许事件冒泡。
Controlling the mouse cursor From the Quartz Display Services programming guide. 控制鼠标光标来自Quartz Display Services编程指南。 Particularly 尤其
CGAssociateMouseAndMouseCursorPosition (false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.