获取有关Control.KeyDown炭?Control、KeyDown

2023-09-03 01:26:28 作者:千与千寻

在处理 Control.OnKey preSS 事件,有一个 包含主要pressEventArgs一个 KeyChar

When handling Control.OnKeyPress event, there is a KeyPressEventArgs that contains a KeyChar.

有关可用性的原因,我需要完全一样的 KeyChar ,但处理时的onkeydown 事件。

For usability reasons I need exactly the same KeyChar but when handling OnKeyDown event.

KeyEventArgs 不包含任何字符有关的数据。我的意思是,如果preSS的 A 键带或不带它不影响键code KEYDATA KEYVALUE 。同样使用另一种语言的时候,我仍然得到资本英语值。

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.

如何获得键pressEventArgs.KeyChar 的KeyDown 事件?

感谢。

推荐答案

转换。下面是简单的功能,将A至A。它转换成唯一的资本字符的 [AZ] 设置。值32是差异'A'和'A'..当然,你可以把它扩展到您的要求,或要求此功能。

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.

char getChar( KeyEventArgs e )
{
 int keyValue = e.KeyValue;
 if ( !e.Shift && keyValue >= (int) Keys.A && keyValue <= (int) Keys.Z )
  return (char)(keyValue + 32);
 return (char) keyValue;
}

如果你需要它与您当前的文化作品,你应该重写ProcessKeyMessage控制方式:

if you need it to works with your current culture you should override ProcessKeyMessage Control method:

protected override bool ProcessKeyMessage( ref Message m )
{
 if ( ( m.Msg == 0x102 ) || ( m.Msg == 0x106 ) ) // this is special - don't remove
 {
  char c = (char) m.WParam; // here is your char for OnKeyDown event ;)
 }
 return base.ProcessKeyMessage( ref m );
}

希望它有帮助。

Hope it helpful.

 
精彩推荐
图片推荐