WPF wrong KeyDown ASCII codes

后端 未结 4 946
北荒
北荒 2020-12-21 02:51

I need to get the numeric values of keyboard keys based on ASCII table .As WPF doesn\'t have built in solution I have tried several hacks:

1. This on

4条回答
  •  萌比男神i
    2020-12-21 03:01

    Based on one of the answers mentioned here, I would suggest You to use TextInput (or PreviewTextInput) event to get the letter pressed. It should give You correct values for both upper and lower case letters.

    In order to test it, You can use code similar to this one:

    private void Window_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        Console.WriteLine(String.Format("letter: {0} (charcode: {1})", e.TextComposition.Text, (int)Convert.ToChar(e.TextComposition.Text)));
    }
    

    Let me know if this helps or if You have any problems when using this solution.

提交回复
热议问题