3

I have a situation where I need to handle a keypress event, it can be any from A-Z, 0-9 but I need to handle that in ViewModel. WHich key is pressed should be passed nito the ViewModel. I am using prism 6.0 so the View1ViewModel is Auto-wired with the view1.

I know that for specific keys I can handle but how to do the same for all keys.

I have a property in ViewModel "KeyPressed", and I tried the below code in code-behind

private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
{
    _mainWindowViewModel.KeyPressed = e.Key;
}

But this code doesn't update the View1ViewModel as in Prism framework viewmodel and view binding is through the framework. I have tried to use interaction, I put the below code in the root Grid section.

<i:Interaction.Triggers >
   <i:EventTrigger EventName="KeyUp">
      <i:InvokeCommandAction Command="{Binding KeyUpEventCommand}" 
        CommandParameter="..."/>
    </i:EventTrigger>
</i:Interaction.Triggers>

The event gets fired but as there is no Command parameter so its null. I need to pass a commandParameter, but don't know what it should be!

In ViewModel:

public DelegateCommand KeyUpEventCommand { get; private set; }

private string strAlphabet;
public string Alphabets
{
     get { return strAlphabet; }
     set { SetProperty(ref strAlphabet, value); }
}

public MainWindowViewModel(IEventAggregator eventAggregator)
{
     KeyUpEventCommand = new DelegateCommand(KeyUpEventHandler);
}

public void KeyUpEventHandler()
{
     //This logic can be changed
     if (Alphabets == "A")
     {
         // Perform some action
     }
     if (Alphabets == "B")
     {
         // Perform some other action
     }
}

Any ideas/suggestions how can I handle this?

Debhere
  • 1,055
  • 2
  • 19
  • 41
  • What exactly does "doesn't update the View1ViewModel" mean? What happens? Does the PreviewKeyUp event handler gets invoked but the setter of the KeyPressed property of the view model doesn't get called or what? – mm8 Feb 13 '17 at 18:10
  • I have updated my question, the KeyUpEventCommand gets fired but it doesn't contain the value of the key press. – Debhere Feb 13 '17 at 18:18
  • How is the KeyUpEventCommand in the view model class defined exactly? – mm8 Feb 13 '17 at 18:25
  • I have updated my code. – Debhere Feb 13 '17 at 18:34
  • Can you add your CommandParameter? – leapold Feb 13 '17 at 18:34

2 Answers2

5

The type of the command property should be DelegateCommand<KeyEventArgs>:

public DelegateCommand<KeyEventArgs> KeyUpEventCommand { get; private set; }

private string strAlphabet;
public string Alphabets
{
    get { return strAlphabet; }
    set { SetProperty(ref strAlphabet, value); }
}

public MainWindowViewModel(IEventAggregator eventAggregator)
{
    KeyUpEventCommand = new DelegateCommand<KeyEventArgs>(KeyUpEventHandler);
}

public void KeyUpEventHandler(KeyEventArgs args)
{
    //...
}

Also make sure that you are using the Prism's own InvokeCommandAction class and don't set the CommandParameter property of it:

xmlns:prism="http://prismlibrary.com/"
...
<i:Interaction.Triggers >
    <i:EventTrigger EventName="PreviewKeyDown">
        <prism:InvokeCommandAction Command="{Binding KeyUpEventCommand}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

Then the KeyEventArgs should be passed as a command parameter by default.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Perfect!!! This is what I was needed. Thanks a lot for the answer. But Can you give some light why prism:InvokeCommandAction doesn't fire if I use some other key, say I press A first then I want to press B then C ... the KeyUpEventCommand should be executed ever time I press a key. But this is not happening. – Debhere Feb 13 '17 at 19:00
  • 1
    Because that class ,which is part of the Blend SDK, doesn't support passing the EventArgs to the command. – mm8 Feb 13 '17 at 19:01
1

Try KeyTriger

    <i:Interaction.Triggers>
        <ei:KeyTrigger Key="Escape">
            <i:InvokeCommandAction Command="{Binding KeyDownCommand}" />
        </ei:KeyTrigger>
    </i:Interaction.Triggers>





public ICommand KeyDownCommand
    {
        get { return new RelayCommand(KeyboardKeyDownCommand, true); }
    }

    private void KeyboardKeyDownCommand()
    {
        ///code
    }
leapold
  • 133
  • 1
  • 10