views:

1710

answers:

3

I'm trying to fire commands based on keystrokes in Silverlight. As I understand you cannot use AccessKey or AcceleratorKey in Silverlight. Also it looks like the might be helpful attached property InputBindings does not work either.

I started looking in other places. It looked like Prism was the way to get commands working in Silverlight, so I checked that out. However they only have a Click handler, which isn't even a useful starting point for getting key commands set up.

Am I just missing some part of Prism? Or is there a good standard way of handling hotkeys with MVVM Silverlight?

A: 

Do you mean like Ctrl+v or such I have seen the following example at the MSDN site.

void Canvas_KeyUp(object sender, KeyEventArgs e)
{
    //check for the specific 'v' key, then check modifiers
    if (e.Key==Key.V) { 
        if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) {
        //specific Ctrl+V action here
        }
    } // else ignore the keystroke
}
Tzury Bar Yochay
I'm looking for an MVVM command pattern. I already know how to hook it up in the codebehind.
RandomEngy
A: 

The MVVM toolkit contains an interesting CommandReference class that allows you to bind InputBindings to ViewModel commands. I'm not sure it works for Silverlight, but you can give it a try...

OK, as RandomEngy pointed out, there are no InputBindings in Silverlight...

However, I think you could use attached behaviors. It's a way to "bind" an event to a command of the ViewModel. Marlon Grech has a good implementation here

Thomas Levesque
I don't believe Silverlight has InputBindings.
RandomEngy
You're absolutely right, I had forgotten this detail... I'm always astounded when I see the limitations of Silverlight compared to WPF
Thomas Levesque
+5  A: 

It sounds like you're looking for a "codeless" MVVMish way of handling the KeyUp/KeyPress/KeyDown event.

Option #1: Prism.
You've mentioned it only ships with the Click command. However, you can add your own attached DPs to enable commands for whatever event you'd like (like KeyUp/KeyDown/KeyPress).

If you're looking for a sample on that Corey has a good one for ToggleButton.Checked/Unchecked events.
http://www.85turns.com/2009/06/24/togglebutton-command-for-prism/

<ToggleButton x:Name="ToggleButton1" 
   customCommands:Checked.Command="{Binding CheckedCommand}"
   customCommands:UnChecked.Command="{Binding UnCheckedCommand}"
        Margin="8,8,0,8" Content="Check me"
        />

Also, Erik Mork has an excellent video that gives you a good overview on commands and how to create a custom command Attached DP. http://development-guides.silverbaylabs.org/Video/Prism-Commands

Option #2: Blend Triggers
The Expression Blend SDK ships with Triggers and Behaviours that are spot on to what you're try to do.
Blend Examples codeplex project ships with a EventTrigger you could use:

<i:EventTrigger EventName="Click">
      <si:InvokeDataCommand Command="{Binding ShoppingCart.CheckOutCommand}"/>
</i:EventTrigger>

Or, you could create your own custom Trigger for Key stroke events and do there whatever you'd like. Here's a sample:
http://azurecoding.net/blogs/brownie/archive/2009/04/06/blend-behaviors-ftw.aspx

JustinAngel
I second Justin's response. I have created a KeyDown/KeyUp command attachment using Option #2 and I handle PageUp/PageDown and other hot keys in my SL app. It works really well. I pass the key code in through the command parameter, so my command can handle the key codes proprly.It is a good idea to create a mass of these attached commands. They are simple to write, and easy to use anywhere you need them.
Brian Genisio
As for Option #1: The Prism model works well for events that need no additional information, but breaks down if you want to have variations of that same event, like different keys on KeyDown. Its implementation relies on only one attached property per element, but for handling hotkeys you will want multiple attached properties on the same element. The attached property behavior implementation does not allow this.Option #2/Brian: Ideally wouldn't you want the hotkeys to be specified in the view, rather than in the command implementation?
RandomEngy
I really like that blend behavior sample. That's one smart guy there ;)
Mike Brown
really good answer
almog.ori