我正在尝试捕获在我的 Silverlight 2 应用程序中发生的任何击键。没有任何输入字段,我正在编写游戏,并且需要知道正在按下哪个箭头键。我对如何通过事件处理程序捕获这些的最佳猜测是不成功的。有什么建议吗?
4 回答
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 事件。
Silverlight 2 支持 KeyUp 和 KeyDown 事件(我相信只有 - 不是 KeyPress)。
在这些事件中,您将获得一个 KeyEventArgs 对象作为参数。您可以从中获取 KeyCode 或 KeyData。
在审查来自不同论坛的几个不同主题时,似乎最好在页面(根)元素上处理您的键事件,解析键以获得所需的效果,并将您的命令重定向到特定控件。
页
this.KeyDown += (s, e) =>
{
MyControl control = (MyControl)Layoutroot.FindName("controlname");
if(control != null)
{
control.MyPublicFunction(e.Key);
}
};
我的控制
public MyPublicFunciton(Key pressedKey)
{
if(pressedKey == Key.Enter)
{
//Do something
}
}