获取 Control.KeyDown 上的字符?
处理 Control.OnKeyPress
事件时,有一个包含 KeyChar
的 KeyPressEventArgs
。
出于可用性原因,我需要完全相同的 KeyChar
但在处理 OnKeyDown
事件时。
KeyEventArgs
不包含任何与字符相关的数据。我的意思是,如果按 A
键,无论是否有 Shift
,它都不会影响 KeyCode
、KeyData
或 键值
。使用另一种语言时也是如此,我仍然获得大写英语值。
如何在 KeyDown
事件中获取 KeyPressEventArgs.KeyChar
?
谢谢。
When handling Control.OnKeyPress
event, there is a KeyPressEventArgs
that contains a KeyChar
.
For usability reasons I need exactly the same KeyChar
but when handling OnKeyDown
event.
The KeyEventArgs
does not contains any char related data. I mean, if press the A
key with or without Shift
its not affecting the KeyCode
, KeyData
or KeyValue
. The same when using another language, i still getting capital english values.
How to get a KeyPressEventArgs.KeyChar
inside KeyDown
event?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
转换它。这是将“A”转换为“a”的简单函数。它仅转换 [AZ] 集中的大写字符。值 32 是“A”和“a”之间的差异。当然您可以将其扩展到您的要求,或者在此处请求功能。
如果您需要它与您当前的文化兼容,您应该重写 ProcessKeyMessage Control 方法:
希望它有帮助。
Convert it. Here is simple function for converting 'A' to 'a'. It converts only capital chars from [A-Z] set. The value 32 is diff between 'A' and 'a'.. Sure you can extend it to your requirements, or ask for feature here.
if you need it to works with your current culture you should override ProcessKeyMessage Control method:
Hope it helpful.
我认为您正在寻找的实际上是 KeyCode 属性。
如果您只是寻找某些键,您可以设置一个 switch 语句或任何您想要的东西。
I think that what you are looking for is in fact the KeyCode property.
If you are just looking for certain keys you could setup a switch statment or whatever your heart desires.
如果您想要使用 KeyDown 而不是 KeyPress 的原因是捕获提供给 KeyDown 事件的额外信息,您是否可以在 KeyDown 事件中捕获该额外信息,然后在获取 KeyPress 时使用它?可以肯定的是,这是一种做作的方法,但我不确定是否有任何其他方法真正适用于具有“死键”(即产生单个字符的两个键序列)的键盘映射。我不知道为什么微软没有在 KeyPress 事件(或其许多事件)中提供更多信息,因为尝试匹配因果事件并不完美,但我不知道真的不知道有什么更好的选择。
If the reason you're wanting to use KeyDown instead of KeyPress is to capture the extra information that's given to a KeyDown event, could you perhaps capture that extra information in a KeyDown event and then use it when you get the KeyPress? Rather a hokey approach, to be sure, but I'm not sure of any other approach that will really work in a keyboard mapping that has "dead keys" (i.e. two-key sequences that produce a single character). I don't know why Microsoft didn't carry along more information through to the KeyPress event (or a lot of its events, for that matter) since trying to match up cause-and-effect events isn't perfect, but I don't really know any better alternative.