Member Avatar for cool_intentions
cool_intentions

I have app with only one button in grid. If I launch mi app and my button has focus my code works pretty much fine except I can’t detect when space is pressed. If I don’t select button first, nothing happens. How can I get pressed key regardless which element on my form has focus? I will later have multiple pages and should be able to catch any pressed key regardless of which page I’m on.
Here is my code:

using System;
using Windows.UI.Popups;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

namespace KeyUpEventApp
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.KeyUp += new KeyEventHandler(MainPage_KeyUp);
        }
        private async void MainPage_KeyUp(object sender, KeyRoutedEventArgs e)
        {
            var key = e.Key;
            var message = new MessageDialog(key.ToString());
            await message.ShowAsync();
        }
    }
}

Anny suggestions?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.