I'm working on a game, a Windows Store App based on WPF and written in C#. When the player presses the Esc key, I want to pause the game and show a menu (Continue, Quit etc.).
Sounds simple. Sadly, it's not.
The game takes place in a Windows.UI.Xaml.Controls.Page and primarily consists of hundreds of Shapes in a Canvas, but no single Button, TextBox or anything else that supports keyboard interaction. The only interaction is clicking or tapping shapes.
I need to catch keyboard events, globally for the whole page, no matter what element has the focus or if there even is any focus at all etc. Whenever the Esc key is pressed, an event has to fire.
What I tried:
Using the event
Page.KeyDownor overridingPage.OnKeyDown(KeyRoutedEventArgs e)(or KeyUp): Does not fire, unless there is an element such as aTextBoxwith keyboard focus. But in my UI there is no such element.Using an invisible (
Opacity = 0and/or hidden under the Canvas) TextBox as a hack to make KeyDown work: As soon as the Canvas or any Shape is clicked/tapped, the TextBox loses focus and the hack stops working. So, more hacks are needed to make it keep the focus, which screws with other things such as the menu buttons. Futhermore, the TextBox occasionally shows the Windows software keyboard, which is rather unwanted. A barely working, fragile hack.Using
InputGestures,KeyBindingetc.: Not available for Windows Store Apps.
Any ideas or solutions?