Silverlight KeyDowns from Javascript
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>
精彩评论