views:

358

answers:

3

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.

A: 

Did you make sure that the Flash element has (HTML) keyboard focus?

Wevah
That's what I'm trying to figure out here. Setting it as first responder doesn't seem to have worked.
starkos
`_myView` points to the plugin view?
Wevah
What I mean is, you might need to set the object/embed element to the focused HTML element in the HTML/JS kind of way. I'm not totally sure how WebViews determine their focused element when they're plugin views.
Wevah
A: 

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.

starkos
A: 

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;
}
starkos