基本 WinForm KeyDown 事件处理

发布于 2024-10-04 02:16:37 字数 247 浏览 5 评论 0原文

我正在使用 WinForms。我为主窗体的 KeyDown 事件创建了一个事件处理程序,从而调用按钮的 Click 事件处理程序。

调用的 Click 事件处理程序取决于按下的特定键。如果用户单击该按钮而不是使用该键,然后随后尝试使用该键,则该键(例如向下箭头)将充当制表符循环,在窗体上的每个按钮控件之间更改焦点(而不是执行Keydown 处理程序)。

有什么想法吗?

I'm using WinForms. I've created an event handler for the KeyDown event of the main form, thereby invoking a button's Click event handler.

The Click event handler called is dependent upon the specific key pressed. If a user clicks the button rather than using the key, and then subsequently tries to use the key thereafter, the key (down arrow for example) acts as a tab-cycle, changing focus between each button control on the form (rather than executing the Keydown handler).

Any ideas ?

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

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

发布评论

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

评论(4

椵侞 2024-10-11 02:16:37

问题是,按钮在被单击时具有焦点,因此后续的按键不会被表单本身捕获,而是被按钮捕获。在按钮的单击事件处理程序中,将窗体聚焦:

this.Focus();

这样,焦点将恢复到窗体,以便窗体将侦听按键事件。

编辑

正如您所发现的,真正的问题是箭头键不被视为输入键。要解决此问题,您需要创建一个新类来继承您要使用的任何控件。然后,您重写 IsInputKey 方法以将箭头键视为输入键。检查此链接:http://bytes.com/topic /c-sharp/answers/517530-trapping-arrow-keys-usercontrol。本文也很有用: http://msdn .microsoft.com/en-us/library/system.windows.forms.control.isinputkey.aspx

The problem is, the button has the focus when it is clicked, so subsequent key presses are not caught by the form itself, but by the buttons instead. In the click event handler for the buttons, focus the form:

this.Focus();

That way, focus is restored to the form so the form will listen for the keypress events.

Edit

The real problem, as you have discovered, is that arrow keys are not treated as input keys. To fix this, you need to create a new class that inherits whatever control you want to use. Then, you override the IsInputKey method to treat arrow keys as input keys. Check this link: http://bytes.com/topic/c-sharp/answers/517530-trapping-arrow-keys-usercontrol. This article is also useful: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.isinputkey.aspx.

俯瞰星空 2024-10-11 02:16:37

根据 SimpleCoder,我必须重写 Button 类的 IsInputKey 成员。

public class ControlButton : Button
{
    protected override bool IsInputKey(Keys keyData)
    {
        if (keyData == Keys.Up)
        {
            return true;
        }
        else if (keyData == Keys.Down)
        {
            return true;
        }
        else if (keyData == Keys.Left)
        {
            return true;
        }
        else if (keyData == Keys.Right)
        {
            return true;
        }
        else
        {
            return base.IsInputKey(keyData);
        }
    }
}

然后我需要使用这个新类实例化我的按钮对象(在设计器类中),如下所示:

    private ControlButton btnDown;
    private ControlButton btnRight;
    private ControlButton btnLeft;
    private ControlButton btnUp;

    this.btnDown = new ControlButton();
    this.btnRight = new ControlButton();
    this.btnUp = new ControlButton();
    this.btnLeft = new ControlButton();

接下来,我为每个新按钮对象注册了 OnClick 处理程序,如下所示:(

    this.btnUp.Click += new System.EventHandler(this.btnUp_Click);

    private void btnUp_Click(object sender, EventArgs e)
    {            
        MessageBox.Show("Up");
    }

等等)

并注册主窗体的 KeyDown 处理程序:

    this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmUavController_KeyDown);

    private void frmUavController_KeyDown(object sender, KeyEventArgs e)
    {
        if ((e.KeyCode == Keys.Up) || (e.KeyCode == Keys.W))
        {
            btnUp.PerformClick();
        }
        else if ((e.KeyCode == Keys.Down) || (e.KeyCode == Keys.S))
        {
            btnDown.PerformClick();
        }
        else if ((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.A))
        {
            btnLeft.PerformClick();
        }
        else if ((e.KeyCode == Keys.Right) || (e.KeyCode == Keys.D))
        {
            btnRight.PerformClick();
        }
    }

将主窗体属性 KeyPreview 设置为 true,并且看起来好像我已经覆盖了向上、向下、向左和右键,按钮控件不再循环焦点,而是返回 true,将控制权转移回主窗体。从这里开始,如果按下后续键(上、下、左或右),表单将执行适当的处​​理程序。

As per SimpleCoder, I had to override the IsInputKey member for the Button class.

public class ControlButton : Button
{
    protected override bool IsInputKey(Keys keyData)
    {
        if (keyData == Keys.Up)
        {
            return true;
        }
        else if (keyData == Keys.Down)
        {
            return true;
        }
        else if (keyData == Keys.Left)
        {
            return true;
        }
        else if (keyData == Keys.Right)
        {
            return true;
        }
        else
        {
            return base.IsInputKey(keyData);
        }
    }
}

Then I needed to instantiate my button objects (in the designer class) using this new class, like so:

    private ControlButton btnDown;
    private ControlButton btnRight;
    private ControlButton btnLeft;
    private ControlButton btnUp;

    this.btnDown = new ControlButton();
    this.btnRight = new ControlButton();
    this.btnUp = new ControlButton();
    this.btnLeft = new ControlButton();

Next I registered OnClick handlers for each of the new button objects like so:

    this.btnUp.Click += new System.EventHandler(this.btnUp_Click);

    private void btnUp_Click(object sender, EventArgs e)
    {            
        MessageBox.Show("Up");
    }

(etc.)

And registered a KeyDown handler for the main form:

    this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmUavController_KeyDown);

    private void frmUavController_KeyDown(object sender, KeyEventArgs e)
    {
        if ((e.KeyCode == Keys.Up) || (e.KeyCode == Keys.W))
        {
            btnUp.PerformClick();
        }
        else if ((e.KeyCode == Keys.Down) || (e.KeyCode == Keys.S))
        {
            btnDown.PerformClick();
        }
        else if ((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.A))
        {
            btnLeft.PerformClick();
        }
        else if ((e.KeyCode == Keys.Right) || (e.KeyCode == Keys.D))
        {
            btnRight.PerformClick();
        }
    }

Having set the main form property KeyPreview to true, and seeing as though I had overridden the default behaviour of the Up, Down, Left and Right keys, the button controls no longer cycle focus, but rather return true, transferring control back to the main form. From here, if subsequent keys (up, down, left or right) are pressed, the form actions the appropriate handler.

染年凉城似染瑾 2024-10-11 02:16:37

使用 Control.GetNextControl 并设置焦点它返回什么。

Use Control.GetNextControl and Set Focus to what it returns.

请你别敷衍 2024-10-11 02:16:37

查看 KeyPreview 属性。 http://msdn.microsoft.com/en -us/library/system.windows.forms.form.keypreview.aspx

将其设置为 true

或者您可能想要覆盖表单的ProcessKeyPreview

Look at KeyPreview property. http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx

Set it to true.

Or alternatively you may want to override ProcessKeyPreview of the form.

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