Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uwp page not firing keydown event at all

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;
        }
    }
}
like image 257
Muhammad Touseef Avatar asked Feb 24 '18 12:02

Muhammad Touseef


1 Answers

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
  }
}
like image 145
Ken Tucker Avatar answered Sep 19 '22 22:09

Ken Tucker