views:

1703

answers:

6

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?

A: 

This tutorial from kirupa.com explains everything you need to know:

This works in WPF, but I am only assuming it's the same with Silverlight.

TandemAdam
A: 

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

Michael S. Scherotter
A: 

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.

Demi
+1  A: 

Hey Jeff:

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.

OdeToCode
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. } } }}
Jeff Blankenburg
Wow does that not cut + paste well.
Jeff Blankenburg
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.)
Jeff Blankenburg
Ah - wire up the event at the UserControl level instead (this.KeyDown += ...).
OdeToCode
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.
Jeff Blankenburg
A: 

Have you had a look at the Blue Rose games tutorials? Link

Graeme Bradbury
A: 

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
    }
}