1

我正在尝试捕获在我的 Silverlight 2 应用程序中发生的任何击键。没有任何输入字段,我正在编写游戏,并且需要知道正在按下哪个箭头键。我对如何通过事件处理程序捕获这些的最佳猜测是不成功的。有什么建议吗?

4

4 回答 4

1

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.

于 2009-06-05T02:26:50.360 回答
0

处理根网格上的 KeyDown 和/或 KeyUp 事件。

于 2009-06-05T00:52:36.417 回答
0

Silverlight 2 支持 KeyUp 和 KeyDown 事件(我相信只有 - 不是 KeyPress)。

在这些事件中,您将获得一个 KeyEventArgs 对象作为参数。您可以从中获取 KeyCode 或 KeyData。

于 2009-06-05T00:53:49.237 回答
0

在审查来自不同论坛的几个不同主题时,似乎最好在页面(根)元素上处理您的键事件,解析键以获得所需的效果,并将您的命令重定向到特定控件。

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
    }
}
于 2009-07-23T11:31:15.700 回答