private void Page_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Right)
{
if (shipPosition < right)
{
x = x + 10;
shipPosition = shipPosition + 10;
ship.Margin = new Thickness(shipPosition, y, 0, 0);
}
}
}
XAML - KeyDown="Page_KeyDown"
I have a rectangle named ship, and above is the function to move it, to be executed on right or left arrow key button press. For some reason, this doesn’t work. The “KeyDown=”Page_KeyDown”” is the xaml that links the button press with the event. There is another grid containing buttons that moves every time a timer ticks, and when I click on a button and highlight it, then press the arrow keys, the ship starts to move, with the highlighted button being changed as well. Does anyone have any idea what is going on?
Update: I removed the is focusable property on the buttons, and that then stopped ship movement all together. So I think it is something to do with the changing of focus on buttons.
Try this :
Put it in the appropriate method of your control (constructor, initialized, …)