-->

Basic WinForm KeyDown event handling

2019-05-19 00:55发布

问题:

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 ?

回答1:

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.



回答2:

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.



回答3:

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



回答4:

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.