如何在WebKit中将键事件发送到Flash电影? [英] How do I send key events to a Flash movie in WebKit?

查看:103
本文介绍了如何在WebKit中将键事件发送到Flash电影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建特定于网站的浏览器应用程序。我创建了一个新的Cocoa应用程序,添加一个WebView到我的窗口,并加载页面。页面包含Flash影片,可通过键盘控制。我想连接一些菜单命令来触发动作,可能是通过模拟击键。

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.

我已经遍历视图层次结构,发现包含NSView电影(类​​名为WebHostedNetscapePluginView)。但我坚持在发送钥匙的点。我尝试使用CGEventCreateKeyboardEvent(),但输入继续到顶级WebView而不是包含电影的子视图。

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.

我试过[[webView窗口] makeFirstResponder:_myView ]设置输入的目标。

I tried [[webView window] makeFirstResponder: _myView] to set the target for the input.

这里是CGEventCreateKeyboardEvent()正确的函数,如果是这样,我如何获得它到我的目标NSView?

Is CGEventCreateKeyboardEvent() the right function here and, if so, how do I get it to my target NSView?

非常感谢。

推荐答案

我的原始答案只有在窗口处于活动状态时才有效。

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.

这适用于普通键(Enter,Space,箭头键等)。 )。它使用键码,因此它不适用于可能根据用户的语言和区域移动的字母和符号。

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.

它不处理修饰键,如Command 。

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

您必须先在浏览器中找到Flash窗口小部件;传递你的WebView到这个。

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

这篇关于如何在WebKit中将键事件发送到Flash电影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆