views:

17

answers:

2

I'm trying to add a KeyDown event handler to the LayoutRoot of a view in a Silverlight SketchFlow project, but it doesn't seem to fire.

The event fires if I change the event type to MouseLeftButton, but I want to demonstrate this using a keyboard-shortcut.

Does anyone know how to accomplish both?

+1  A: 

Well, for some reasons not obvious to the uninvited, the KeyDown event did not fire when specified in XAML. I found that hooking onto the the Application.Current.RootVisual.KeyDown in code-behind does the trick.

Kristoffer Deinoff
A: 

The reason is that your LayoutRoot is actually not "the" layout root when hosted in a SketchFlow player. Keyboard focus is initially on the Sketchflow player.

You might try adding a Focus() call in your page loaded event, but also make sure you have added the Jscript to initially focus to the actual browser Silverlight object first. e.g.

<script type="text/javascript">
        function appLoad(sender, args) {
            var xamlObject = document.getElementById('SilverlightObject');
            if (xamlObject != null)
                xamlObject.focus();
        }

and

<object id='SilverlightObject' data= ...
            [snip]
            <param name="onError" value="onSilverlightError" />
            <param name="onLoad" value="appLoad" />

If you don't have that code in the HTML/ASPX page hosting a Silverlight app, all keypresses go the browser instead.

Enough already