Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa: Limit mouse to screen

I'm developing a kiosk mode application for OSX. In some circumstances, another screen gets attached. My application runs in fullscreen on one screen using:

[self.window.contentView enterFullScreenMode:s 
                                 withOptions:[NSDictionary dictionaryWithObject:appOptions
                                      forKey:NSFullScreenModeApplicationPresentationOptions]];

The options are the following:

[NSNumber numberWithUnsignedInt:(NSApplicationPresentationHideMenuBar|
                                NSApplicationPresentationHideDock|
                                 NSApplicationPresentationDisableHideApplication|
                                 NSApplicationPresentationDisableProcessSwitching|
                                 NSApplicationPresentationDisableAppleMenu)];

What I want is limit the mouse cursor to the screen where the game is running.

How can I accomplish that?

like image 444
jsadfeew Avatar asked Feb 25 '23 13:02

jsadfeew


1 Answers

Add an NSTrackingArea to the screens you don't want the mouse entering. When you get notified that the mouse has entered the tracking area, use CGEventCreateMouseEvent and CGPostEvent to move the mouse back to a safe location, probably the nearest point on the main screen.

like image 71
BJ Homer Avatar answered Mar 08 '23 01:03

BJ Homer