获取 Control.KeyDown 上的字符?

发布于 2024-09-25 05:41:37 字数 470 浏览 6 评论 0原文

处理 Control.OnKeyPress 事件时,有一个包含 KeyCharKeyPressEventArgs

出于可用性原因,我需要完全相同的 KeyChar 但在处理 OnKeyDown 事件时。

KeyEventArgs 不包含任何与字符相关的数据。我的意思是,如果按 A 键,无论是否有 Shift,它都不会影响 KeyCodeKeyData键值。使用另一种语言时也是如此,我仍然获得大写英语值。

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧时模样 2024-10-02 05:41:37

转换它。这是将“A”转换为“a”的简单函数。它仅转换 [AZ] 集中的大写字符。值 32 是“A”和“a”之间的差异。当然您可以将其扩展到您的要求,或者在此处请求功能。

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 Control 方法:

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 );
}

希望它有帮助。

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;
}

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.

口干舌燥 2024-10-02 05:41:37

我认为您正在寻找的实际上是 KeyCode 属性。

private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Determine whether the key entered is the F1 key. If it is, display Help.
    if(e.KeyCode == Keys.A)
    {
        // Do something kewl
    }
    else if(e.KeyCode == Keys.B)
    {
        // Do something even kewler
    }
}

如果您只是寻找某些键,您可以设置一个 switch 语句或任何您想要的东西。

I think that what you are looking for is in fact the KeyCode property.

private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Determine whether the key entered is the F1 key. If it is, display Help.
    if(e.KeyCode == Keys.A)
    {
        // Do something kewl
    }
    else if(e.KeyCode == Keys.B)
    {
        // Do something even kewler
    }
}

If you are just looking for certain keys you could setup a switch statment or whatever your heart desires.

那小子欠揍 2024-10-02 05:41:37

如果您想要使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文