Convert a System.Windows.Input.KeyEventArgs key to a char

It takes a little getting used to, but you can just use the key values themselves. If you're trying to limit input to alphanumerics and maybe a little extra, the code below may help.

    private bool bLeftShiftKey = false;
    private bool bRightShiftKey = false;

    private bool IsValidDescriptionKey(Key key)
    {
        //KEYS ALLOWED REGARDLESS OF SHIFT KEY

        //various editing keys
        if (
        key == Key.Back ||
        key == Key.Tab ||
        key == Key.Up ||
        key == Key.Down ||
        key == Key.Left ||
        key == Key.Right ||
        key == Key.Delete ||
        key == Key.Space ||
        key == Key.Home ||
        key == Key.End
        ) {
            return true;
        }

        //letters
        if (key >= Key.A && key <= Key.Z)
        {
            return true;
        }

        //numbers from keypad
        if (key >= Key.NumPad0 && key <= Key.NumPad9)
        {
            return true;
        }

        //hyphen
        if (key == Key.OemMinus)
        {
            return true;
        }

        //KEYS ALLOWED CONDITITIONALLY DEPENDING ON SHIFT KEY

        if (!bLeftShiftKey && !bRightShiftKey)
        {
            //numbers from keyboard
            if (key >= Key.D0 && key <= Key.D9)
            {
                return true;
            }
        }

        return false;
    }

    private void cboDescription_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.LeftShift)
        {
            bLeftShiftKey = true;
        }

        if (e.Key == Key.RightShift)
        {
            bRightShiftKey = true;
        }

        if (!IsValidDescriptionKey(e.Key))
        {
            e.Handled = true;
        }
    }

    private void cboDescription_PreviewKeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.LeftShift)
        {
            bLeftShiftKey = false;
        }

        if (e.Key == Key.RightShift)
        {
            bRightShiftKey = false;
        }
    }

See How to convert a character in to equivalent System.Windows.Input.Key Enum value? Use KeyInterop.VirtualKeyFromKey instead.


That work for me:

Based on the last entry i found that in WPF there is no such event PreNotifyInput, but i found and equivalent PreviewTextInput

First I try with a RegExp, but I cant make it work, then I use a simple indexOf.

private bool ValidChar(string _char)
{
   string Lista = @" ! "" # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ";
   return Lista.IndexOf(_char.ToUpper()) != -1;
   //System.Text.RegularExpressions.Regex RegVal = new System.Text.RegularExpressions.Regex(@"(?<LETRAS>[A-Z]+)+(?<NUMERO>[0-9]+)+(?<CAR>[!|""|#|$|%|&|'|(|)|*|+|,|\-|.|/|:|;|<|=|>|?|@]+)+");
   //return RegVal.IsMatch(_char);
}

private void textBoxDescripcion_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    if (!ValidChar(e.Text))
         e.Handled = true;
}