locked
KeyUp/KeyDown events on combobox RRS feed

  • Question

  • The KeyUp and KeyDown events on a combobox don't seem to fire for an "Enter" keypress. 

    Instead, the Enter keypress opens the combobox's dropdown.  Is there a way that I can receive the "Enter" keyup/keydown events on a Silverlight combobox?

     Also, in one of my Silverlight projects, the KeyUp/Keydown events seem to fire twice for each keypress on a combobox.  In another project it only fires once....is this a known bug?  Is there a workaround?

     

    Wednesday, April 8, 2009 10:19 AM

Answers

  • Hi,

    I've found certain Silverlight Controls absord certain key presses and do not pass them through using the key events.  I think the only solution here is to create a custom class that inherits from ComboBox and override the OnKeyDown() and OnKeyUp() methods.

    This is the custom class I created and can be used in exactly the same way as a normal ComboBox:

    public class MyComboBox : ComboBox
    {
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // Process your enter key press here
            }
            else
            {
                base.OnKeyDown(e);
            }
        }
    
        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // Process your enter key press here
            }
            else
            {
                base.OnKeyDown(e);
            }
        }
    }
    Wednesday, April 8, 2009 10:28 AM

All replies

  • Hi,

    I've found certain Silverlight Controls absord certain key presses and do not pass them through using the key events.  I think the only solution here is to create a custom class that inherits from ComboBox and override the OnKeyDown() and OnKeyUp() methods.

    This is the custom class I created and can be used in exactly the same way as a normal ComboBox:

    public class MyComboBox : ComboBox
    {
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // Process your enter key press here
            }
            else
            {
                base.OnKeyDown(e);
            }
        }
    
        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // Process your enter key press here
            }
            else
            {
                base.OnKeyDown(e);
            }
        }
    }
    Wednesday, April 8, 2009 10:28 AM
  • Thanks for the quick reply.  That works great. 

     The only problem I have still is that every keypress comes in twice for some reason...so I wrote some code to ignore every other keypress.

    Wednesday, April 8, 2009 11:08 AM
  • Thats wierd, it may be a bug in the combo box but the best way to get around it is to set e.Handled = true the first time it hits and it will not call it twice.
    Wednesday, April 8, 2009 11:18 AM
  • Thanks, thats better than what I had initially to deal with the duplicate key problem.

    Wednesday, April 8, 2009 11:39 AM