I've been trying to make this work as part of a game to learn the UWP enviroment when using Canvas. I've read so much on the subject of getting KeyDown to work I'm lost.
Needless to say I hope but none of them seem to work, none cause the event handler to be called.
Any help would be appreciated.
My XAML
<Grid Background="Black">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Canvas Name="paintCanvas" Background="Black"
Grid.Column="1" HorizontalAlignment="Stretch" MaxWidth="642" MaxHeight="422"/>
</Grid>
My simplified code behind:
namespace Game
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
paintCanvas.KeyDown += new KeyEventHandler(OnKeyDownHandler);
}
private void OnKeyDownHandler(object sender, KeyRoutedEventArgs e)
{
switch (e.Key)
{
case VirtualKey.Down:
// do something
break;
case VirtualKey.Up:
// do something
break;
case VirtualKey.Left:
// do something
break;
case VirtualKey.Right:
// do something
break;
}
}
}
}