Keypress To Simulate A Button Click in C#

Cistoran picture Cistoran · Nov 27, 2009 · Viewed 23k times · Source

Ok, so I'm in the process of making a Tic-Tac-Toe game to help me learn C#. I'm attempting to add a bit of functionality to it, so I want people to be able to use the NumPad on a computer to simulate clicking the buttons.

Here is what I have but when I use the NumPad the buttons don't click. Can any of you see a reason as to why?

    //===============================
    // start NumPad Simulate Clicks
    //   NumPad  MyButtons
    //   7 8 9   1 2 3
    //   4 5 6   4 5 6 
    //   1 2 3   7 8 9
    //===============================
    public void myControl_NumPad7(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad7)
        {
            button1_Click(null, null);
        }
    }
    public void myControl_NumPad8(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad8)
        {
            button2_Click(null, null);
        }
    }
    public void myControl_NumPad9(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad9)
        {
            button3_Click(null, null);
        }
    }
    public void myControl_NumPad4(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad4)
        {
            button4_Click(null, null);
        }
    }
    public void myControl_NumPad5(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad5)
        {
            button5_Click(null, null);
        }
    }
    public void myControl_NumPad6(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad6)
        {
            button6_Click(null, null);
        }
    }
    public void myControl_NumPad1(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad1)
        {
            button7_Click(null, null);
        }
    }
    public void myControl_NumPad2(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad2)
        {
            button8_Click(null, null);
        }
    }
    public void myControl_NumPad3(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.NumPad3)
        {
            button9_Click(null, null);
        }
    }

Answer

Thorsten Dittmar picture Thorsten Dittmar · Nov 27, 2009

EDIT
Noticed that I have to be clearer about what I mean...

From the code you posted I suspect you have 9 controls you added your key-events to. These controls will only receive key-events when they are focused.

To handle keys globally for the form, you have to set Form.KeyPreview to true. Also, I'd not handle the keys the way you do, but add a Form.KeyDown event and write something like:

switch (e.KeyCode)
{
    case Keys.NumPad9:
        e.Handled = true;
        button3.PerformClick();
        break;
    case Keys.NumPad8:
        e.Handled = true;
        button2.PerformClick();
        break;
    // And so on
}

This will handle the NumPad-Keys within the form - you can then remove all the event handlers you posted in your question.

To programatically "click" a button, you should use the Button.PerformClick() method, as more than one event handler may be added to the click event, which would not be called otherwise.

EDIT 2
Syntax for the switch-statement was invalid. Of course every "case" must start with the case keyword... Now it should work.