如何在 Silverlight 应用程序中捕获击键?
我正在尝试捕获 Silverlight 2 应用程序中发生的任何击键。 没有任何输入字段,我正在编写一个游戏,需要知道按下了哪个箭头键。 我对如何通过事件处理程序捕获这些的最佳猜测是不成功的。 有什么建议吗?
I am trying to capture any keystroke that happens inside my Silverlight 2 application. There are not any input fields, I'm writing a game, and need to know which of the arrow keys are being pressed. My best guesses at how to capture these through an event handler are unsuccessful. Any recommendations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
很高兴再次见到你(虚拟)。
我假设您已经尝试在根 UserControl 元素上连接 KeyDown。 以下是一些提示:
插件在看到关键事件之前需要聚焦。 您必须强制用户单击插件才能启动。
确保没有其他元素(例如 ScrollViewer)正在占用箭头键事件。 如果您正在使用 ScrollViewer,您只会看到 KeyUp。
不切换到全屏模式。
您一定缺少这样简单的东西。 希望有帮助。
Good to see you again (virtually).
I'm assuming you already tried wiring KeyDown on the root UserControl element. Here are some tips:
The plugin needs focused before it sees key events. You'll have to force the user into clicking on the plugin to start.
Make sure you don't have another element (like the ScrollViewer) that is eating arrow keys events. If you have a ScrollViewer in play you'll only be seeing KeyUp.
No switching to full screen mode.
Must be something simple you are missing like that. Hope that helps.
处理根网格上的 KeyDown 和/或 KeyUp 事件。
Handle the KeyDown and/or the KeyUp event on your root Grid.
Silverlight 2 支持 KeyUp 和 KeyDown 事件(我认为仅支持 - 不支持 KeyPress)。
在这些事件中,您将获取 KeyEventArgs 对象作为参数。 您可以从中获取 KeyCode 或 KeyData。
Silverlight 2 supports the KeyUp and KeyDown events (only, I believe - not KeyPress).
In these events, you get a KeyEventArgs object as a parameter. You can get the KeyCode or KeyData from this.
在查看来自不同论坛的有关此问题的几个不同线程时,似乎最好在页面(根)元素上处理键事件,解析键以获得所需的效果,并将命令重定向到特定控件。
页面
我的控制
In reviewing a few different threads from different forums regarding this matter it would seem it is best to handle your keyevents on your Page (root) element, parse the key for the desired effect, and redirect your command to the particular control.
Page
MyControl