views:

183

answers:

2

I have this:

<Canvas x:Name="LayoutRoot" KeyDown="LayoutRoot_KeyDown">
</Canvas>

In a newly-minted Ag 3 application in VS2008. I simply changed the default <Grid /> to <Canvas /> and added a KeyDown handler that pops a MessageBox.

But no matter how I tried, the KeyDown event just would never, ever fires.

Is it simply that <Canvas /> doesn't support KeyDown or am I doing something wrong?

+2  A: 

You need to have at least something inside the Canvas that can receive focus, and you will find that the event will bubble up.

You may also want to set IsHitTestVisible="True" on the Canvas.

Jeff Wilcox
+1  A: 

Since your Canvas is the "LayoutRoot" you could use the containing UserControl instead.

Place the property IsTabStop="true" on the UserControl. In the Loaded event call this.Focus(). Attach your KeyDown handler to the UserControl instead of the Canvas.

As long as the plugin itself has the focus (which you can ensure with a little Javascript) you should now get key events.

AnthonyWJones