基本WinForm KeyDown事件处理 [英] Basic WinForm KeyDown event handling

查看:610
本文介绍了基本WinForm KeyDown事件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



任何想法?

解决方案

根据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;
私人ControlButton btnUp;

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

下一步我注册了$ code> OnClick 处理程序新的按钮对象如下所示:

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

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

(等)



  

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,看到我已经覆盖了Up,Down,Left和Right键的默认行为,按钮控件不再循环焦点,而是返回true,将控件传回主要形式。从这里,如果按下后续键(向上,向下,向左或向右),窗体将执行相应的处理程序。


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 ?

解决方案

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.

这篇关于基本WinForm KeyDown事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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