WPF错误的KeyDown ASCII码 [英] WPF wrong KeyDown ASCII codes

查看:450
本文介绍了WPF错误的KeyDown ASCII码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据 ASCII表得到键盘按键的数值。至于 WPF没有内置的解决方案,我尝试了好几种黑客:

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 这其中只给出大写值

        char res ;
        Char.TryParse(e.Key.ToString(),out res);
        Debug.WriteLine((int)res);



2 这一次,尽管它在很多地方尽可能上市。解决方案,给出了一个完全错误的号码

2 This one ,even though it is listed in many places as possible solution , gives a completely wrong number .

        Debug.WriteLine(Convert.ToInt16(e.Key).ToString());



那么,如何获得在WPF输入大写和小写字母的ASCII码?

So how do I get both upper and lower case letter ASCII codes from the input in WPF ?

更新

在这里得到一些答案后,我想强调我的问题。我绝对必须从输入ASCII码都为下限和上限case.No硬编码的字符比较或类似that.I事情正在寻找更通用的方法。

After getting several answers here I want to emphasize my question .I absolutely must get ASCII codes from the input both for lower and upper case.No hard coded char compare or anything like that.I am looking for more generic way.

推荐答案

根据这里提到的答案之一,我建议你​​使用的TextInput (或 PreviewTextInput )事件得到按字母。它应该给你的大写和小写字母的正确值

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.

这篇关于WPF错误的KeyDown ASCII码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆