1

So I have the following KeyBindings:

<Page.InputBindings>
    <KeyBinding Key="Space" Command="{Binding AcceptCommand}" />
    <KeyBinding Key="Esc" Command="{Binding CancelCommand}"/>
</Page.InputBindings>

I also use an attached behavior to propagate the InputBindings to the ancestor Window, since if I don't there are focus issues and the commands don't always get called when I want them to. Here's a link to the handy method of doing that.

My problem is that the KeyBindings happen on KeyDown, and I want them to happen on KeyUp. From what I've read, that's just not possible and you instead have to handle the KeyUp event and do everything from code-behind. While that's not ideal I'll do it if I have to, however I don't know how I'd propagate the KeyUp from the Page to the Window.

I tried making an attached behavior similar to the one for the input bindings, but the nature of events is I can't detach events nor check whether the event is null unless I'm in the actual class that owns the event (in this case, Page).

Anyone have any ideas on how to accomplish this?

Community
  • 1
  • 1
StuartMorgan
  • 658
  • 5
  • 28

1 Answers1

0

I made the following KeyUpBinding from InputBinding, in case you need to add more dependency properties as CommpandParameter just add as an object:

 public class KeyUpBinding : InputBinding
{
    public Key Key
    {
        get { return (Key)GetValue(KeyProperty); }
        set { SetValue(KeyProperty, value); }
    }

    public static readonly DependencyProperty KeyProperty =
        DependencyProperty.Register("Key", typeof(Key), typeof(KeyUpBinding), new PropertyMetadata(Key.A, KeyChanged));

    private static void KeyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var keybinding = (d as KeyUpBinding);

        Keyboard.AddKeyUpHandler(App.Current.MainWindow, (s, ku) =>
        {
            if(keybinding.Command!=null && ku.Key == keybinding.Key && ku.IsUp)
            {
                keybinding.Command.Execute(null);
            }
        });
    }


    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), typeof(KeyUpBinding), new PropertyMetadata(null));


}
Juan Pablo Garcia Coello
  • 3,192
  • 1
  • 23
  • 33
  • This looks promising... Wouldn't it inherit the Command, CommandParameter, etc properties from InputBinding, eliminating the need for adding them? Also I tried using this basically the same way as my KeyBindings above but as KeyUpBindings instead, and the KeyChanged function is never getting called. My use is slightly different because my WPF project is a DLL and I'm instantiating a NavigationWindow to use instead of App.Current.MainWindow, but I doubt that's the reason since it's never getting to that point anyways. – StuartMorgan Apr 16 '15 at 22:37
  • You mean inherit keybinding, it has not option for activating the event handler (might be you find a way) and then it will call twice one for down and in case you can one for up – Juan Pablo Garcia Coello Apr 17 '15 at 04:58