在Windows应用商店的应用程序页面全局键盘事件 [英] Page-global keyboard events in Windows Store Apps

查看:164
本文介绍了在Windows应用商店的应用程序页面全局键盘事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个游戏,一个Windows Store应用基于WPF和C#编写的。当玩家按下Esc键,我想暂停游戏,并显示一个菜单(继续,退出等)。

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.

游戏的故事发生在 Windows.UI.Xaml.Controls.Page ,主要由数百名形状 S IN一个画布,但没有一个按钮文本框或其他任何支持键盘交互。唯一的互动是点击或点击形状。

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.

我要赶键盘事件,在全球整个页面,无论是有焦点或哪些因素甚至有任何。重点在所有等当Esc键被按下时,一个事件有火

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.

我想什么:


  • 使用事件 Page.KeyDown 或覆盖 Page.OnKeyDown(KeyRoutedEventArgs E)(或的KeyUp):不火,除非有一个元素,如文本框键盘焦点。但在我的UI也没有这样的元素。

  • Using the event Page.KeyDown or overriding Page.OnKeyDown(KeyRoutedEventArgs e) (or KeyUp): Does not fire, unless there is an element such as a TextBox with keyboard focus. But in my UI there is no such element.

使用一个无形的(不透明度= 0 和/或Canvas)的文本框下隐藏着一个黑客,使工作的KeyDown:只要帆布或任何形状点击/挖掘,文本框失去焦点和黑客停止工作。这样,需要更多的黑客,使其保持的焦点,它与其他的东西螺钉诸如菜单按钮。 Futhermore,文本框偶尔显示了Windows软件键盘,这是相当有害的。勉强工作,脆弱的黑客。

Using an invisible (Opacity = 0 and/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.

使用 InputGestures 键绑定等:不适用于Windows应用商店的应用程序。

Using InputGestures, KeyBinding etc.: Not available for Windows Store Apps.

任何意见或解决方案?

推荐答案

尝试使用的 CoreWindow.KeyDown 。分配处理程序在你的页面,我相信它应该拦截所有的keydown事件。

Try using CoreWindow.KeyDown. Assign the handler in your page and I believe it should intercept all keydown events.

public MyPage()
{
    CoreWindow.GetForCurrentThread().KeyDown += MyPage_KeyDown;
}

void MyPage_KeyDown(CoreWindow sender, KeyEventArgs args)
{
    Debug.WriteLine(args.VirtualKey.ToString());
}

这篇关于在Windows应用商店的应用程序页面全局键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆