如何从WPF KeyDown事件中获取正常字符? [英] How do I get the normal characters from a WPF KeyDown event?

查看:163
本文介绍了如何从WPF KeyDown事件中获取正常字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要从WPF KeyDown 事件的 e.Key 属性传递的ASCII字符。

I want the ASCII characters passed by the e.Key property from a WPF KeyDown event.

推荐答案

不幸的是没有简单的方法来做到这一点。有两个解决方法,但是它们都在某些条件下都会掉落。

Unfortunately there's no easy way to do this. There's 2 workarounds, but they both fall down under certain conditions.

第一个是将其转换为字符串:

The first one is to convert it to a string:

TestLabel.Content = e.Key.ToString();

这将给你一些像CapsLock和Shift等的东西,但是在字母数字键的情况下,那么当时还不能告诉你这个班级的状况等等,所以你必须自己弄清楚。

This will give you the things like CapsLock and Shift etc, but, in the case of the alphanumeric keys, it won't be able to tell you the state of shift etc. at the time, so you'll have to figure that out yourself.

第二种选择是要使用TextInput事件,其中e.Text将包含输入的实际文本。这将给你字母数字键的正确字符,但它不会给你控制字符。

The second alternative is to use the TextInput event instead, where e.Text will contain the actual text entered. This will give you the correct character for alphanumeric keys, but it won't give you control characters.

这篇关于如何从WPF KeyDown事件中获取正常字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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