WinForms,处理所有键= OnKeyPress + OnKeyDown,但是如何呢?

发布于 2024-08-12 06:17:00 字数 384 浏览 6 评论 0原文

我有一个表单,自定义绘图,没有控件。

当我需要从用户那里获取输入时,我转到 OnKeyDown 事件,然后将事件的 KeyData 映射到字符串** - 这样我就可以识别出用户已按下“ctrl X”等,所以一切都几乎没问题......问题在于不同的键盘设置和语言特定字母 - 所以今天我的用户得到了

ã ->; a

ó -> o

所以我需要 KeyPress 事件,对吗?但是我怎么知道用户输入的是“ł”而不是 alt+L?我现在暂时无法理解这个问题...

** - 我需要这样做,因为我让用户配置快捷方式并使用 .ToString() 生成不太用户友好的文本,例如“OemPeriod,Control” ”或者更好:“ControlKey,Control”

I've got one Form, custom drawing, no controls.

When I need to get the input from the user I go to OnKeyDown event and then map event's KeyData to a string** - so that I can recognize that user has pressed "ctrl X", etc, so everything is almost ok... the problem is with different keyboard settings and language specific letters - so for today my users get

ã -> a

ó -> o

So I need the KeyPress event right? But how do I know that user typed in for example 'ł' and not alt+L? I can't get my head around that for a while now...

** - I need to do it like that since I let users to configure shortcuts and using .ToString() produces not so user friendly text like "OemPeriod, Control" or even better: "ControlKey, Control"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

层林尽染 2024-08-19 06:17:00

是的,您需要 KeyPress 事件。或者更确切地说,重写 OnKeyPress。将 KeyDown 事件中的虚拟键代码映射到击键非常困难,您必须了解当前的键盘布局。查看 ToUnicodeEx() 的 MSDN 文档< /a> 看看你面临的是什么。

您不必担心 Alt+L 等组合键。它们不生成 KeyPress 事件。

这是一个例子。启动一个新的 Windows 窗体项目并使代码如下所示:

using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }
    StringBuilder mText = new StringBuilder();

    protected override void OnKeyPress(KeyPressEventArgs e) {
      if (e.KeyChar == '\b') {
        if (mText.Length > 0) mText.Remove(mText.Length - 1, 1);
      }
      else mText.Append(e.KeyChar);
      Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e) {
      TextFormatFlags fmt = TextFormatFlags.Left;
      TextRenderer.DrawText(e.Graphics, mText.ToString(), this.Font, this.ClientRectangle, Color.Black, fmt);
    }
  }
}

Yes, you'll need the KeyPress event. Or rather, override OnKeyPress. Mapping virtual key codes in the KeyDown event to key strokes is quite difficult, you'd have to be aware of the current keyboard layout. Take a look at the MSDN docs for ToUnicodeEx() to see what you are up against.

You don't have to worry about keystroke combinations like Alt+L. They don't generate a KeyPress event.

Here's an example. Start a new Windows Forms project and make the code look like this:

using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }
    StringBuilder mText = new StringBuilder();

    protected override void OnKeyPress(KeyPressEventArgs e) {
      if (e.KeyChar == '\b') {
        if (mText.Length > 0) mText.Remove(mText.Length - 1, 1);
      }
      else mText.Append(e.KeyChar);
      Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e) {
      TextFormatFlags fmt = TextFormatFlags.Left;
      TextRenderer.DrawText(e.Graphics, mText.ToString(), this.Font, this.ClientRectangle, Color.Black, fmt);
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文