locked
AutomationPeer to Fire KeyDown Event RRS feed

  • Question

  • I have a behavior attached to a ComboBox to handle KeyDown events and select the first item starting with the letter pressed.  http://www.reflectionit.nl/Blog/PermaLinkd137c1f7-a515-4084-8199-f8b3cf892b8f.aspx The problem with this implementation is that once the combobox is expanded (and the combobox's Popup is shown) pressing the key no longer does anything.  I have found this is because the Popup/ItemsPresenter is handling the key event, and not the ComboBox anymore.  I am trying to get key support for when it is expanded.

    The approach I am taking (and it very well could be wrong) is that I have created a TargetedTriggerAction<Selector>, attached it to the ItemsPresenter KeyDown method and now I want to "fire" the KeyDown of the Selector with the same KeyEventArgs as the ItemsPresenter received.  How do you fire events programmatically?  In WPF it looks there are RaiseEventXXX methods, but nothing like that in Silverlight.  Here is an example of "firing" the click event for a button http://geekswithblogs.net/SilverBlog/archive/2009/09/21/behaviors-textbox-enter-button-invoke-targetedtriggeraction.aspx It uses AutomationPeer which seems like should be able to do something similar but do it for key events....... alas, I have searched and searched and read and read to no avail.  Does anyone know how to raise a Key event from AutomationPeer?  It has to be possible, the automation libraries are what they use to do UI testing to simulate user input, right?

    I REALLY appreciate any help on this.  I do not simply want to handle the KeyDown event of the ItemsPresenter unless absolutely necessary (I want to reuse the KeyboardSelectionBehavior).  Thanks!

    Friday, January 28, 2011 2:01 PM

All replies

  • Hi,

    AutomationPeer (or more correctly, AutomationElement - one doesn't generally use the AutomationPeer directly) doesn't deal with keyboard input directly.  It was able to Invoke the click event in the geekswithblogs sample because that is a logical action within the scope of the Automation system.

    AutomationElements can set the value or selection that would be set by the keyboard, but they don't directly synthesize input events.

    Have you looked at using the AutoCompleteBox rather than devising your own control?  It sounds like it does essentially what you are looking for.   You can also look at the source for the AutoCompleteBox in the Silverlight Toolkit to see how it is implemented.

     

    Regards

    Sunday, February 13, 2011 8:54 PM
  • I would really like to know the answer to this as well.

    Automating a button click is easy but it seems as though it is not easy for key down.

    Monday, September 23, 2013 12:57 AM