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

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.

Related

updateTrackingAreas: override only works for the first 2 times?

On El Capitan in Xcode 7 beta using Swift 2.0, I subclassed a NSView to use as a prototype view of NSCollectionView's item view, and override the updateTrackingAreas: method to do the mouse tracking. The NSCollectionView was inside a NSPopover.
It seems that only the first 2 times the updateTrackingAreas: will be called, as the debug log shows. The code was like the following:
override func updateTrackingAreas() {
Swift.print("updateTrackingAreas:")
if trackingArea != nil {
self.removeTrackingArea(trackingArea!)
}
trackingArea = NSTrackingArea(
rect: self.bounds,
options: [NSTrackingAreaOptions.MouseEnteredAndExited, NSTrackingAreaOptions.ActiveAlways],
owner: self,
userInfo: nil
)
if trackingArea != nil {
self.addTrackingArea(trackingArea!)
}
var mouseLocation = self.window?.mouseLocationOutsideOfEventStream
mouseLocation = self.convertPoint(mouseLocation!, fromView: nil)
if CGRectContainsPoint(self.bounds, mouseLocation!) {
mouseEntered(NSEvent())
} else {
mouseExited(NSEvent())
}
super.updateTrackingAreas()
}
The first 2 times when the popover was opened, I can see the console log shows updateTrackingAreas:. Then the tracking failed and no logs either.
EDIT:
When I commented out this part like:
//var mouseLocation = self.window?.mouseLocationOutsideOfEventStream
mouseLocation = self.convertPoint(mouseLocation!, fromView: nil)
if CGRectContainsPoint(self.bounds, mouseLocation!) {
mouseEntered(NSEvent())
} else {
mouseExited(NSEvent())
}
The problem will no longer exists.
And the following code makes no differences:
if let window = self.window {
var mouseLocation = window.mouseLocationOutsideOfEventStream
mouseLocation = self.convertPoint(mouseLocation, fromView: nil)
if let event = NSApplication.sharedApplication().currentEvent {
if NSPointInRect(mouseLocation, self.bounds) {
mouseEntered(event)
} else {
mouseExited(event)
}
}
}
EDIT 2:
Ok, I just reinstall OS X Yosemite and compiled it again with Xcode 7 beta 1. The problem no longer exists. Might just be a bug of El Capitan. I'll report it to Apple. Thank you all.
You're passing a newly-created event instance to mouseEntered() and mouseExited(). I realize that this is because you can't pass nil in the Swift variant of the method but perhaps you should pass the current event instead (NSApplication and NSWindow both have a currentEvent() method). Have you tried this?
Checking the mouseLocation in updateTrackingAreas is senseless.
From the docs of updateTrackingAreas:
Invoked automatically when the view’s geometry changes such that its tracking areas need to be recalculated.
This is usually due to frame change. It will be called as many times as you view changes it's frame or position. I doubt you've implemented live-resizing/moving feature on your view, this is the only way mouse inside view's frame can be the source of your view's geometry change.
Ok, I just reinstall OS X Yosemite and compiled it again with Xcode 7 beta 1. The problem no longer exists.
Might just be a bug of El Capitan. Already reported it to Apple.
Thank you all.

Check if NSAnimationContext runAnimationGroup cancelled or succeeded

I'm animating a view (by revealing it) after which I need to post a notification (once the animation completes). However the way the app's designed, there's another notification sent out when the view is hidden (via animation). So essentially I have a 'showView' and a 'hideView' method. Each do something like so:
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
[context setDuration: 0.25];
[[myView animator] setAlphaValue: 0];
} completionHandler:^{
// post notification now
}];
The problem is that in another thread in the background, I have a periodic timer which performs a 'sanity check' to see if the view needs to be hidden if it's not needed. It's an aggressive approach but necessary as many components of the app respond to different events and scenarios. Due to this I at times get a 'race condition' where the user has clicked to 'showView' but at the same time one of our threads feel the view should be immediately hidden.
When both post a notification on the main thread, the app hangs indefinitely in a SpinLock. I could completely avoid this situation if I was able to figure out if the animation block above was 'cancelled' (i.e. another animation block executed on the same view). In such situations I would not post the notification, which would then not force a check.
Long story short: I need to be able to check if the 'completionHandler' was called after animation successfully ended or if it was cancelled. I know in iOS this is possible but I can't find any way to do this in OS X. Please help.
I encountered a similar problem. I solved it by using a currentAnimationUUID instance variable.
NSUUID *animationUUID = [[NSUUID alloc] init];
self.currentAnimationUUID = animationUUID;
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
// set properties to animate
} completionHandler:^{
BOOL isCancelled = ![self.currentAnimationUUID isEqual:animationUUID];
if (isCancelled) {
return;
}
self.currentAnimationUUID = nil;
// do stuff
}];
Swift version:
let animationUUID = UUID()
currentAnimationUUID = animationUUID
NSAnimationContext.runAnimationGroup({ context in
// set properties to animate
}, completionHandler: {
let isCancelled = self.currentAnimationUUID != animationUUID
guard !isCancelled else {
return
}
self.currentAnimationUUID = nil
// do stuff
})

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");
}
}];

Set activity indicator until image is loaded from server?

I'm new to iOS development so please bear with me.
I'm making a photo-grid using a table view and scroll view.
My question is how could I load an activity indicator until an image downloads from a server and then display the image and remove the activity indicator?
I'm trying to stay away from third-party library's as I want to understand how it works.
Place an Activity Indicator (via Interface Builder or manually) on your view. Set the property to "hide when not animating".
When doing server call, call [activityIndicator startAnimating] (IBOutlet property).
When returning with actual image, call [activityIndicator stopAnimating]. When stopping, it wil automatically hide.
You can also use the activity indicator in the iPhon/Pad status bar. To do this, use [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
Set to NO for hiding it ... (obviously)
Take a look at dowloading an image async for a sample of dl'ing an image.
You would stop the activity indicator in the didReceiveData function.
https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCsQFjAA&url=https%3A%2F%2Fgithub.com%2Fjakemarsh%2FJMImageCache&ei=UEp7U_W2GoWB8gWShIGgBQ&usg=AFQjCNEc0a59K2wEOlZ2IbapWhVc87kHmg&bvm=bv.67229260,d.dGc
here you could find JMImage cache files that you could use for downloading images.
You have to change UIImageView+JMImageCache.m file
if(i) {
dispatch_async(dispatch_get_main_queue(), ^{
safeSelf.jm_imageURL = nil;
safeSelf.image = i;
[safeSelf setNeedsLayout];
[safeSelf setNeedsDisplay];
if (completionBlock) {
completionBlock(i).
}
});
then use method
enter code here
{
[yourImageView setImageWithURL:[NSURL URLWithString:urlString] placeholder:[UIImage imageNamed:#"placeholder"] completionBlock:^(UIImage *image)
{
// remove added activity indicator here
}failureBlock:^(NSURLRequest *req,NSURLResponse *resp,NSError *error)
{
// show error message
}];
}

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.