如何在C#中以用户控件的形式获取所有KeyPress事件? [英] How to get all KeyPress events as a UserControl in C#?

查看:331
本文介绍了如何在C#中以用户控件的形式获取所有KeyPress事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经阅读了很多有关按键事件的文章,但是我不知道如何获得它们.我知道,只有具有键盘焦点的当前控件才会获得按下事件.但是如何确保我的用户控件具有它?

I've read quite some articles now about key press events but I can't figure out how to get them. I know that only the current control with keyboard focus gets the press events. But how can i ensure that my user control has it?

尝试了一下却没有运气:

Tried this without luck:

public partial class Editor : UserControl

this.SetStyle(ControlStyles.Selectable, true);
this.TabStop = true;
...
//take focus on click
protected override void OnMouseDown(MouseEventArgs e)
{
    this.Focus();
    base.OnMouseDown(e);
}
...
protected override bool IsInputKey(Keys keyData)
{
   return true;
}

还有这个:

//register global keyboard event handlers
private void Editor_ParentChanged(object sender, EventArgs e)
{
    if (TopLevelControl is Form)
    {
        (TopLevelControl as Form).KeyPreview = true;
        TopLevelControl.KeyDown += FCanvas_KeyDown;
        TopLevelControl.KeyUp += FCanvas_KeyUp;
        TopLevelControl.KeyPress += FCanvas_KeyPress;
    }
}

后者给了我按键向下和向上的事件,但仍然没有按键.当我的控件继承自UserControl时,我还可以使用其他任何方法来获取每个down/up/press事件吗?

The latter gave me the key down and up events, but still no key press. Is there any other method i can use to just get every down/up/press events when my control inherits from UserControl?

就像有一条评论链接了另一个SO问题一样:我还要获取KeyPress事件,这一点很重要,因为无论使用哪种语言,它都会在每个键盘上发送正确的字符.这对于文字编写很重要.如果仅获得单个键,则必须自己将其处理为正确的字符.但是也许有一种便捷的方法可以将按下的键转换为本地字符?

As there was a comment linking another SO question: It's important that I also get the KeyPress event, since it sends the correct character on every keyboard no matter which language. This is important for text writing. If you only get the individual keys you have to process them into the correct character on your own. But maybe there is a convenience method to transform the pressed keys into a localized character?

推荐答案

  • 将父窗体的(包含用户控件)KeyPreview属性设置为true
  • 将新的KeyPress事件添加到您的父表单中
    • Set your parent form's (contains the usercontrol) KeyPreview property to true
    • Add a new KeyPress event to your parent form
    • 设置父窗体的按键事件以将事件转发到您的用户控件:

      Set the parent form keypress event to forward the event to your usercontrol:

      private void parentForm_KeyPress(object sender, KeyPressEventArgs e)
              {
                  // Forward the sender and arguments to your usercontrol's method
                  this.yourUserControl.yourUserControl_KeyPress(sender, e);
              }
      

      yourUserControl1_KeyPress方法替换为您自己的方法,您想在用户每次按下按钮(按下按钮然后释放按钮)时运行该方法.

      Replace the yourUserControl1_KeyPress method with your own method, which you want to run each time the user presses a button (the button is pressed down and then released).

      您还可以为用户控件创建一个新的KeyPress处理程序,然后在该示例中转发sender和KeyPressEventArgs对象.

      You can also create a new KeyPress handler to your usercontrol, and forward the sender and KeyPressEventArgs objects there, as in this example.

      这篇关于如何在C#中以用户控件的形式获取所有KeyPress事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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