Limiting mouse to one display on Mac (Potentially using Cocoa) - objective-c

I've been feverishly searching for a method by which to limit the user's mouse to one display in a multi-display setup on a Mac.
I've stumbled upon this question: Cocoa: Limit mouse to screen, I promise I have not duplicated this question.
The question did, however, spark an idea in my mind that it might be possible to write a simple application using Cocoa to restrict the mouse to one screen, run this application in the background, and still use my game which has been developed in AS3/Adobe AIR/Flash.
The game is a full-screen game, and will always be at the same resolution on the same monitor. The other monitor will also always have the same resolution, but is to be just a non-interactive display of information. I need the user to be able to interact with the game, but not accidentally move the mouse off of the game's screen.
Question Summary:
Can I create a basic application for Mac OS X (Lion) using Cocoa/Objective C that will restrict the mouse to one monitor that can be run IN THE BACKGROUND and prevent users from moving the mouse outside of the monitor that will have a full-screen game running on it?
[EDIT:]
I found the basic code necessary to run a loop for the Quartz Event Filter, courtesy of this answer: Modify NSEvent to send a different key than the one that was pressed
I am going to use that code for testing purposes, and I have modified it a bit to detect mouse events like so:
CGEventRef mouse_filter(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
NSPoint point = CGEventGetLocation(event);
NSPoint target = NSMakePoint(100,100);
if (point.x >= 500){
CGEventSetLocation(event,target);
}
return event;
}
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
CFRunLoopSourceRef runLoopSource;
CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, kCGEventMouseMoved, mouse_filter, NULL);
if (!eventTap) {
NSLog(#"Couldn't create event tap!");
exit(1);
}
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
CFRelease(eventTap);
CFRelease(runLoopSource);
[pool release];
exit(0);
}
This, however, doesn't seem to work quite right. It is certainly detecting mouse events properly, and it moves the events properly as well. For instance, if I drag a window past 500 pixels from the left of the screen, the window drag event gets moved to 100,100. However, the mouse itself doesn't get moved to the new location when it is simply being moved around the screen, performing no other actions. IE: You can still move the mouse all around the screen, instead of just on the left 500 pixel column.
Any ideas?

Use a Quartz Event Tap to filter mouse moved and mouse dragged events. In the tap callback, use CGEventSetLocation to modify the location of mouse events that would otherwise move off the main screen.
You may need to run the program as root, or have assistive device access enabled in System Preferences.
Quartz Event Services Reference

Related

Simulate Click and Drag in OS X

I am trying to simulate mouse action via programming.
I am not able to do click and drag action reliably.
For click and drag I am doing the following actions in the order mentioned below
Post left mouse button pressed event at current mouse pointer position (only once)
Move the mouse (as mouse down event is already sent, it must act as mouse down and drag, selecting text from the initial mouse postion)
Post left mouse button release event to end mouse hold action.
In apple applications such as Xcode, or Safari, this method does not work reliably (It docent highlight text when dragged, but with actual mouse I can highlight text by click and drag). Cannot drag icons either.
However in Firefox, this technique works. The selection follows mouse! That is as we move mouse pointer, the selection also changes accordingly, which is what I need.
The following is the code I use to send mouse tap events.
-(void)_sendMouseEvent:(int)mouseEvent withMouseButton:(CGMouseButton)mouseBtn
{
CGEventRef ourEvent = CGEventCreate(NULL);
NSPoint mouseLoc = CGEventGetLocation(ourEvent); //get current mouse position
CGEventRef mouseClick = CGEventCreateMouseEvent(
NULL,
mouseEvent,
mouseLoc,
mouseBtn
);
CGEventPost(kCGHIDEventTap, mouseClick);
CFRelease(ourEvent);
CFRelease(mouseClick);
}
I did try sending mouse down events at regular intervals, but it made no change.
Do you have any ideas on what I am doing wrong? Please suggest any workarounds that come to your mind.
I found out that we need to send mouse event kCGEventLeftMouseDragged first, to start dragging and selecting.
[self _sendMouseEvent:kCGEventLeftMouseDragged withMouseButton:kCGMouseButtonLeft]; //Start draging.
-(void)_sendMouseEvent:(int)mouseEvent withMouseButton:(CGMouseButton)mouseBtn
{
CGEventRef ourEvent = CGEventCreate(NULL);
NSPoint mouseLoc = CGEventGetLocation(ourEvent); //get current mouse position
CGEventRef mouseClick = CGEventCreateMouseEvent(
NULL,
mouseEvent,
mouseLoc,
mouseBtn
);
CGEventPost(kCGHIDEventTap, mouseClick);
CFRelease(ourEvent);
CFRelease(mouseClick);
}

How can I detect if mouse is down outside of window in cocoa?

As the titles says i wonder if it is possible to detect if the mouse button is down. I tried putting this code in my app delegate.m but with no success.
- (void)mouseDown:(NSEvent *)theEvent
{
NSLog(#"hello world!");
}
A quick google search showed me that this method only works inside of NSWindows. However, there most be some way to detect if mouse position is pressed, and if so; how can i do it?
You can use NSEvent addGlobalMonitorForEventsMatchingMask:
define in your control:
id mouseEventMonitor;
-(id)init{
mouseEventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:(NSLeftMouseDownMask | NSRightMouseDownMask | NSOtherMouseDownMask)
handler:^(NSEvent *event){
NSLog(#"theEvent->%#",event);
//here you will receive the all mouse DOWN events
if (event.modifierFlags & NSCommandKeyMask)
{
NSLog(#"theEvent1->%#",event);
}else{
NSLog(#"theEvent2->%#",event);
}
}];
return self;
}
Cocoa Event Monitors are the way to go.
You can use them to track pretty much every kind of mouse event outside of your view hierarchy.
Check out the documentation in the Cocoa Event Handling Guide for information on how to use them and similar topics here on SO like
In what way a view or a window could know that mouseDown outside itself in Xcode?
How do i detect keystrokes using objective c?
Although you mention mouseDown in your question and have accepted an answer that describes how to set up a monitor, as far as I can tell from the rest of the question, you don't actually need an event that fires when the mouse button is pressed, but instead just want to check if a mouse button is currently pressed.
NSEvent has a class property you can use for this:
[NSEvent pressedMouseButtons]
This returns the indices of the currently pressed mouse buttons:
A return value of 1 << 0 corresponds to the left mouse button, 1 << 1 corresponds to the right mouse button, 1<< n, n >=2 correspond to other mouse buttons.

Cocoa: Middle mouse click event (global) + gesture API?

Are there any resources that might guide me in how to make my app respond to any type of mouse click that is not the left / right button? Globally, even when my app is not active.
And for magic mouse / trackpad, are there any frameworks or resources available to easily attach my code to a specific gesture?
You can find everything related to mouse events:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/HandlingMouseEvents/HandlingMouseEvents.html
and trackpad events:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/HandlingTouchEvents/HandlingTouchEvents.html#//apple_ref/doc/uid/10000060i-CH13-SW10
Here is a stack overflow link that has explanation for handling global events.
Here's a code example based on Shashank's (very helpful) answer.
NSEventMask eventMask = NSOtherMouseDownMask|NSOtherMouseUpMask;
[NSEvent addGlobalMonitorForEventsMatchingMask:eventMask
handler:^(NSEvent *event) {
if (event.type == NSOtherMouseDown) {
NSLog(#"middle click down");
} else if (event.type == NSOtherMouseUp) {
NSLog(#"middle click up");
}
}];

MPMoviePlayerController adding UIButton to view that fades with controls

I am trying to add a UIButton to the view of a MPMoviePlayerController along with the standard controls. The button appears over the video and works as expected receiving touch events, but I would like to have it fade in and out with the standard controls in response to user touches.
I know I could accomplish this by rolling my own custom player controls, but it seems silly since I am just trying to add one button.
EDIT
If you recursively traverse the view hierarchy of the MPMoviePlayerController's view eventually you will come to a view class called MPInlineVideoOverlay. You can add any additional controls easily to this view to achieve the auto fade in/out behavior.
There are a few gotchas though, it can sometimes take awhile (up to a second in my experience) after you have created the MPMoviePlayerController and added it to a view before it has initialized fully and created it's MPInlineVideoOverlay layer. Because of this I had to create an instance variable called controlView in the code below because sometimes it doesn't exist when this code runs. This is why I have the last bit of code where the function calls itself again in 0.1 seconds if it isn't found. I couldn't notice any delay in the button appearing on my interface despite this delay.
-(void)setupAdditionalControls {
//Call after you have initialized your MPMoviePlayerController (probably viewDidLoad)
controlView = nil;
[self recursiveViewTraversal:movie.view counter:0];
//check to see if we found it, if we didn't we need to do it again in 0.1 seconds
if(controlView) {
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[controlView addSubview:backButton];
} else {
[self performSelector:#selector(setupAdditionalControls) withObject:nil afterDelay:0.1];
}
}
-(void)recursiveViewTraversal:(UIView*)view counter:(int)counter {
NSLog(#"Depth %d - %#", counter, view); //For debug
if([view isKindOfClass:NSClassFromString(#"MPInlineVideoOverlay")]) {
//Add any additional controls you want to have fade with the standard controls here
controlView = view;
} else {
for(UIView *child in [view subviews]) {
[self recursiveViewTraversal:child counter:counter+1];
}
}
}
It isn't the best solution, but I am posting it in case someone else is trying to do the same thing. If Apple was to change the view structure or class names internal to the control overlay it would break. I am also assuming you aren't playing the video full screen (although you can play it fullscreen with embeded controls). I also had to disable the fullscreen button using the technique described here because the MPInlineVideoOverlay view gets removed and released when it is pressed: iPad MPMoviePlayerController - Disable Fullscreen
Calling setupAdditionalControls when you receive the fullscreen notifications described above will re-add your additional controls to the UI.
Would love a more elegant solution if anyone can suggest something other than this hackery I have come up with.
My solution to the same problem was:
Add the button as a child of the MPMoviePlayerController's view;
fade the button in and out using animation of its alpha property, with the proper durations;
handle the player controller's touchesBegan, and use that to toggle the button's visibility (using its alpha);
use a timer to determine when to hide the button again.
By trial-and-error, I determined that the durations that matched the (current) iOS ones are:
fade in: 0.1s
fade out: 0.2s
duration on screen: 5.0s (extend that each time the view is touched)
Of course this is still fragile; if the built-in delays change, mine will look wrong, but the code will still run.

How do I send key events to a Flash movie in WebKit?

I am trying to create a site-specific browser application. I've created a new Cocoa app, added a WebView to my window, and am loading up the page. The page contains a Flash movie, which can be controlled via the keyboard. I'd like to wire up some menu commands to trigger the actions, presumably by simulating the keystrokes.
I've traversed the view hierarchy and have found the NSView that contains the movie (with class name "WebHostedNetscapePluginView"). But I'm stuck at the point of sending it keys. I tried using CGEventCreateKeyboardEvent() but the input keeps going to the top-level WebView rather than the subview containing the movie.
I tried [[webView window] makeFirstResponder: _myView] to set the target for the input.
Is CGEventCreateKeyboardEvent() the right function here and, if so, how do I get it to my target NSView?
Many thanks in advance.
My original answer would only work if the window was active. I also wanted it to work when the window was hidden, and came up with the code below.
This works for ordinary keys (Enter, Space, arrow keys, etc.). It uses keycodes so it won't work for letters and symbols that might move around based on the user's language and region.
And it doesn't handle modifier keys like Command. If someone can figure that out I will gladly give them credit for the answer to this question.
// For Safari 4.x+
if ([[flashView className] isEqual:#"WebHostedNetscapePluginView"])
{
CGEventRef event = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)keycode, true);
[flashView keyDown:[NSEvent eventWithCGEvent:event]];
CFRelease(event);
}
else
{
EventRecord event;
event.what = keyDown;
event.message = keycode << 8;
event.modifiers = 0;
// For Safari 3.x
if ([flashView respondsToSelector:#selector(sendEvent:)]) {
[(id)flashView sendEvent:(NSEvent*)&event];
event.what = keyUp;
[(id)flashView sendEvent:(NSEvent*)&event];
}
// For Safari 4.x
else if ([(id)flashView respondsToSelector:#selector(sendEvent:isDrawRect:)]) {
[(id)flashView sendEvent:(NSEvent *)&event isDrawRect:NO];
event.what = keyUp;
[(id)flashView sendEvent:(NSEvent *)&event isDrawRect:NO];
}
else
{
NSLog(#"ERROR: unable to locate event selector for Flash plugin");
}
}
You must first locate the Flash widget in the browser; pass your WebView to this.
- (NSView*)_findFlashViewInView:(NSView*)view
{
NSString* className = [view className];
// WebHostedNetscapePluginView showed up in Safari 4.x,
// WebNetscapePluginDocumentView is Safari 3.x.
if ([className isEqual:#"WebHostedNetscapePluginView"] ||
[className isEqual:#"WebNetscapePluginDocumentView"])
{
// Do any checks to make sure you've got the right player
return view;
}
// Okay, this view isn't a plugin, keep going
for (NSView* subview in [view subviews])
{
NSView* result = [self _findFlashViewInView:subview];
if (result) return result;
}
return nil;
}
Did you make sure that the Flash element has (HTML) keyboard focus?
I managed to figure it out. Here's the code:
[[self window] makeFirstResponder:_tunerView];
CGEventRef e1 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)49, true);
CGEventPost(kCGSessionEventTap, e1);
CFRelease(e1);
CGEventRef e2 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)49, false);
CGEventPost(kCGSessionEventTap, e2);
CFRelease(e2);
window is the main application window. _tunerView is the WebHostedNetscapePluginView. I was setting the first responder when the window first opened, that didn't work. I have to set it right before I send the keys.