none
How to Convert Key to String during KeyDown.

    Question

  • Hello,

    I have a key down handler in my control and I need to convert the Key property of the KeyEventArgs to a string (considering modifers). Is there anyway to do this in WPF ?

    I know WPF will send a TextInput event after the KeyDown (if that key generates text input), but my code needs to know if KeyDown will generete TextInput before it returns from KeyDown.

    In win32, I used ToUnicode to solve this problem. In WPF, calling ToUnicode() causes weird side effects with dead keys.

    Thanks
    Felipe

    Friday, October 20, 2006 9:32 PM

All replies

  • Did you ever receive a reply to this?  I have the same problem.  Thanks for your help.

     

    Richard Fencel

    Thursday, December 21, 2006 7:29 PM
  • I just found out about an event called PreviewTextInput.  It works jim dandy.  I use it for comboboxes.  It gives you the char that was just typed (e.Text), as well as the complete string that was typed up to this time (e.OriginalSource) and allows you to eat the char that was just typed (set e.Handled = true).  This is better than KeyUp which gives you the complete string but e.Handled = true does not eat the char, or KeyDown where e.Handled = true works but you don't get the most recently typed char.

     

    Richard

     

     

    Thursday, December 21, 2006 9:13 PM
  • Regarding my last post, here is the code I used:

     

    public void comboBox1_PreviewTextInput(Object sender, TextCompositionEventArgs e)

    {

    string newText = e.Text;

    TextBox tb = (TextBox)e.OriginalSource;

    string previousText = tb.Text;

    // e.Handled = true; // this eats the new text if you don't want it

    }

    Thursday, December 21, 2006 9:36 PM