locked
Event Trigger implementation for KeyUp and KeyDown with parameter passing RRS feed

  • Question

  • Is there a relatively simple way to use an event trigger to execute a command AND access the key information to determine which key was pressed? If so, can you please share a code example? I am using an MVVM approach and do not want to use the code behind KeyUP or KeyDown event. However, the only workable approach that I found on the web utilized A LOT OF CODE and was very complex, which is also not desirable.

     

    <i:EventTrigger EventName="KeyDown">

         <i:InvokeCommandAction CommandName="MyCommandKeyDown"

                              IsEnabled="True"

                              Command="{Binding MyCommandKeyDown, BindsDirectlyToSource=True}" />

    </i:EventTrigger>

     

    What I am looking for is some way to effectively get: if (parameter.Key == Key.Shift) functionality using an event trigger.

     

    Thanks for your assistance...


    Warren
    Tuesday, December 20, 2011 7:01 PM

Answers

  • Brian,

    What I finally implemented was an application event handler that detects all keyboard events. I then detect and handle those specific keyboard sequences (e.g., Ctrl+Key) accordingly. I wasn't able to figure out a simple MVVM pattern to accomplish this in code for specific controls. If you have any related EventTrigger examples, I would appreciate your sharing them.

    Thanks for your assistance.


    Warren
    • Proposed as answer by Brian Hilstrom Wednesday, December 28, 2011 2:59 PM
    • Marked as answer by highdownts Thursday, December 29, 2011 7:44 PM
    Tuesday, December 27, 2011 2:44 PM

All replies

  • It sounds like you're looking for InputBindings. Check out the MSDN page here: http://msdn.microsoft.com/en-us/library/system.windows.input.inputbinding.aspx

    You'll want to change your Key attribute so it triggers off the Shift key.

    • Marked as answer by highdownts Tuesday, December 20, 2011 9:13 PM
    • Unmarked as answer by highdownts Wednesday, December 21, 2011 6:34 PM
    Tuesday, December 20, 2011 8:54 PM
  • Brian,

    I unchecked the question as answered based upon what I have found out after trying to implement the solution recommended at the link above. I am using Silverlight 4.0 and from what I have read, it does not support Inputbindings (at least the XAML code version). I have been unable to get this functionality working in Silverlight code so I am still looking for a suitable answer. Many posting on the web say that this works for Silverlight, but from what I have been able to discover, it is only supported in WPF and possibly Silverlight 5.0. I only have a few posts that reference Silverlight 5 so I don't know if they are accurate since I cannot switch to version 5 to test the approach now.

     


    Warren
    • Edited by highdownts Wednesday, December 21, 2011 6:42 PM
    Tuesday, December 20, 2011 9:15 PM
  • Brian, I updated my ealier post...
    Warren
    Wednesday, December 21, 2011 6:43 PM
  • Sorry, I should have updated earlier. You are correct: Silverlight 4 does not support the InputBindings class. As such, there isn't an easy way to do this in XAML in Silverlight. Because of that, you're going to be looking at the KeyUp/KeyDown event handlers. Let me know if you have questions about implementing handlers for those events.
    Tuesday, December 27, 2011 2:32 PM
  • Brian,

    What I finally implemented was an application event handler that detects all keyboard events. I then detect and handle those specific keyboard sequences (e.g., Ctrl+Key) accordingly. I wasn't able to figure out a simple MVVM pattern to accomplish this in code for specific controls. If you have any related EventTrigger examples, I would appreciate your sharing them.

    Thanks for your assistance.


    Warren
    • Proposed as answer by Brian Hilstrom Wednesday, December 28, 2011 2:59 PM
    • Marked as answer by highdownts Thursday, December 29, 2011 7:44 PM
    Tuesday, December 27, 2011 2:44 PM