Connor
Connor

Reputation: 64644

Windows 8 Metro focus on grid

I'm trying to make a simple windows 8 metro game using c# and xaml. I've been having trouble getting the KeyDown event to fire. I think this may be because the grid does not have focus. If this is the case, how would I give the grid focus in my code? Thanks for helping.

Here's my code: The grid in xaml

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" 
      KeyDown="Grid_KeyDown" KeyUp="Grid_KeyUp" x:Name="myGrid"
      >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
    <StackPanel Orientation="Vertical">
        <StackPanel Orientation="Horizontal">
            <TextBlock FontSize="30" Margin="10" x:Name="statusBox">Status</TextBlock>
        </StackPanel>
    </StackPanel>

and the event handler in c#

    private void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        statusBox.Text = "Key down";
    }

Upvotes: 2

Views: 1804

Answers (1)

JP Alioto
JP Alioto

Reputation: 45117

Hook the event at the CoreWindow level instead of the grid level. In your C#, do something like ...

CoreWindow.GetForCurrentThread().KeyDown += Window_KeyDown;

Where Window_Keydown is a method in place of where you currently have Grid_KeyDown.

Upvotes: 11

Related Questions