Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard events not firing in RT app

So I've been working on a pet project for the Windows Store, and have hit a bit of a stumbling block: keyboard events refuse to fire. I've tried forcing focus onto the main page, tried forcing focus onto the Grid, but nothing seems to help. Anyone run into issues like this? My google-fu has failed me.

Relevant code:

XAML:

<Page
    x:Class="PlatformTD.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:PlatformTD"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"
          Name="LayoutRoot">
        <Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                Name="MainCanvas" x:FieldModifier="public"
                SizeChanged="MainCanvas_SizeChanged"
                Loaded="MainCanvas_Loaded" />
    </Grid>
</Page>

C#:

public MainPage()
{
    this.InitializeComponent();
    MainCanvas.KeyDown += MainPage_KeyDown;
    MainCanvas.KeyUp += MainPage_KeyUp;
    KeyDown += MainPage_KeyDown;
    KeyUp += MainPage_KeyUp;
    LayoutRoot.KeyDown += MainPage_KeyDown;
    LayoutRoot.KeyUp += MainPage_KeyUp;
}
private void MainPage_KeyUp(object sender, KeyRoutedEventArgs e)
{ // breakpoint here to catch when event fires
    // does stuff
}
private void MainPage_KeyDown(object sender, KeyRoutedEventArgs e)
{ // breakpoint here to catch when event fires
    // does stuff
}
like image 288
muad_dib Avatar asked Apr 09 '13 22:04

muad_dib


1 Answers

How about something like this:

public MainPage()
{
    this.InitializeComponent();
    Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
    Window.Current.CoreWindow.KeyUp += CoreWindow_KeyUp;
}

void CoreWindow_KeyUp(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
    throw new NotImplementedException();
}

void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
    throw new NotImplementedException();
}

Works on my side. Solution found here.

like image 123
Igor Ralic Avatar answered Oct 22 '22 12:10

Igor Ralic