开发者

Silverlight KeyDowns from Javascript

开发者 https://www.devze.com 2023-02-28 23:07 出处:网络
There is any way to listen for the Silverlight plugin pressed keys from the HTML page onKeyDown event? I can have multiple .xap loaded in my HTML and all the .xaps should handle same shortcut 开发者_开

There is any way to listen for the Silverlight plugin pressed keys from the HTML page onKeyDown event? I can have multiple .xap loaded in my HTML and all the .xaps should handle same shortcut 开发者_开发知识库keys.


You can create a proxy via a ScriptableType, unfortunately you have to do this in all your Silverlight applications.

[ScriptableType]
public class KeyPressProxy
{
    public KeyPressProxy()
    {
        App.Current.RootVisual.KeyDown += (s, e) => KeyDown(s, e);
    }

    [ScriptableMember]
    public event KeyEventHandler KeyDown = delegate { };
}

Then you have to register the object, but be sure you do this after the RootVisual has been created:

private void Application_Startup(object sender, StartupEventArgs e)
{
    this.RootVisual = new MainPage();

    var proxy = new KeyPressProxy();
    HtmlPage.RegisterScriptableObject("KeyPressProxy", proxy);
}

Finally you can add a handler in your javascript:

<div id="silverlightControlHost">
    <object id="silverlightApp1" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
        <param name="onLoad" value="onPluginLoaded" />
        <!-- ... -->
    </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
</div>
<script type="text/javascript">
    function onPluginLoaded() {
        document.getElementById('silverlightApp1').content.KeyPressProxy.KeyDown = function (s, e) {
            alert('pressed');
        }
    }
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号