按键模拟 C# 中的按钮点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1807483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:02:12  来源:igfitidea点击:

Keypress To Simulate A Button Click in C#

c#clickkeypresssimulate

提问by Cistoran

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.

好的,所以我正在制作一个井字游戏来帮助我学习 C#。我正在尝试为其添加一些功能,因此我希望人们能够在计算机上使用 NumPad 来模拟单击按钮。

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?

这是我所拥有的,但是当我使用 NumPad 时,按钮不会点击。你们中有人能看出原因吗?

    //===============================
    // 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);
        }
    }

采纳答案by Thorsten Dittmar

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.

从您发布的代码中,我怀疑您有 9 个控件添加了按键事件。这些控件仅在获得焦点时才会接收按键事件。

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

要全局处理表单的键,您必须设置Form.KeyPreviewtrue. 另外,我不会像您那样处理键,而是添加一个Form.KeyDown事件并编写如下内容:

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.

这将处理表单中的 NumPad-Keys - 然后您可以删除您在问题中发布的所有事件处理程序。

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.

要以编程方式“单击”按钮,您应该使用该Button.PerformClick()方法,因为可能会向单击事件添加多个事件处理程序,否则不会调用该方法。

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

编辑 2-statement 的
语法switch无效。当然,每个“案例”都必须以case关键字开头......现在它应该可以工作了。

回答by terR0Q

You need to use button1.PerformClick();for every button to invoke event correctly, here's info.

您需要使用 button1.PerformClick();每个按钮来正确调用事件,这里是info