uwp page not firing keydown event at all
Asked Answered
F

1

7

in my uwp app I just added a new page and navigated to it, and I have nothing on that page just the default grid. but I am trying to recieve keydown events which is not firing up by any key at all. I have also put keydown to the grid on xaml and to the page in code behind, but it is still not firing up I have assured that with break points.

xaml

<Grid KeyDown="VideoPageKeyDown">

</Grid>

code behind

public sealed partial class VideoPlay : Page
{
    public VideoPlay()
    {
        this.InitializeComponent();
        KeyDown += VideoPageKeyDown;
    }

    private void VideoPageKeyDown(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == VirtualKey.GamepadDPadDown
            || e.Key == VirtualKey.GamepadLeftThumbstickDown)
        {
            //VideoMenuGrid.Visibility = Visibility.Visible;
            //await VideoMenuGrid.Offset(200f, 0f, 1000, 0).StartAsync();
        }
        else if (e.Key == VirtualKey.GamepadDPadUp
            || e.Key == VirtualKey.GamepadLeftThumbstickUp)
        {
            //await VideoMenuGrid.Offset(0f, 0f, 1000, 0).StartAsync();
            //VideoMenuGrid.Visibility = Visibility.Collapsed;
        }
    }
}
Falange answered 24/2, 2018 at 12:49 Comment(0)
M
13

Controls like the grid that do not get focus will not receive the event directly. If there is another control on top of it might pass the event to the grid because it is an routed event. Thy using the coreWindow keyDown event instead.

public sealed partial class VideoPlay : Page
{
  public VideoPlay()
  {
     this.InitializeComponent();
     //Add this line
     Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;    
  }

  void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs e)
  {
    //todo
  }
}
Murillo answered 24/2, 2018 at 13:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.