1

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?

4 Answers 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.

5
  • I think I've avoided all of these pitfalls. Here's my code: namespace ClickTheButton { public partial class Level10 : UserControl { public Level10() { // Required to initialize variables InitializeComponent(); Loaded += new RoutedEventHandler(Level10_Loaded); } void Level10_Loaded(object sender, RoutedEventArgs e) { LayoutRoot.KeyDown += new KeyEventHandler(LayoutRoot_KeyDown); } void LayoutRoot_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Right) { //DO THE THING THAT THIS KEY SHOULD DO. } } } } Jun 5, 2009 at 17:22
  • 1
    Wow does that not cut + paste well. Jun 5, 2009 at 17:22
  • I'm attaching the KeyDown event handler here: LayoutRoot.KeyDown += new KeyEventHandler(LayoutRoot_KeyDown); And then in the event handling method, I am just evaluating for the Key value of e. However, my event handling method is never even getting called (and I'm certain I have given focus to my Silverlight app.) Jun 5, 2009 at 17:26
  • Ah - wire up the event at the UserControl level instead (this.KeyDown += ...).
    – OdeToCode
    Jun 6, 2009 at 1:22
  • I'm getting closer. So I think it's the way I've architected my Silverlight app. My page.xaml (default xaml file) loads the content of another xaml file at the beginning. I then change the content over and over, loading a new XAML file for each "level" of my game. The KeyDown event fires perfectly fine for a brand-new app, but I'm clearly not elevating this handler to the right level. But to your credit, you have certainly answered the question I posed. Jun 8, 2009 at 18:48
0

Handle the KeyDown and/or the KeyUp event on your root Grid.

0

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.

0

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

this.KeyDown += (s, e) =>
   {
       MyControl control = (MyControl)Layoutroot.FindName("controlname");
       if(control != null)
       {
           control.MyPublicFunction(e.Key);
       }
   };

MyControl

public MyPublicFunciton(Key pressedKey)
{
    if(pressedKey == Key.Enter)
    {
        //Do something
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.