按键来模拟一个按钮单击C# [英] Keypress To Simulate A Button Click in C#

查看:153
本文介绍了按键来模拟一个按钮单击C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我在制作井字棋游戏来帮助我学习C#的过程。
我试图一点点的功能添加到它,所以我希望人们能够使用数字小键盘的计算机上模拟点击按钮。



下面是我,但是当我使用数字小键盘的按钮不要点击。
任何你能明白了一个道理,为什么?



  // ========= ====================== 
//启动数字小键盘模拟点击
//数字小键盘MyButtons
// 7 8 9 1 2 3
// 4 5 6 4 5 6
// 1 2 3 7 8 9
// ================== =============
公共无效myControl_NumPad7(对象发件人,KeyPressEventArgs E)
{
如果(e.KeyChar ==(炭)Keys.NumPad7)
{
的button1_Click(NULL,NULL);
}
}
公共无效myControl_NumPad8(对象发件人,KeyPressEventArgs E)
{
如果(e.KeyChar ==(char)的Keys.NumPad8)
{
button2_Click(NULL,NULL);
}
}
公共无效myControl_NumPad9(对象发件人,KeyPressEventArgs E)
{
如果(e.KeyChar ==(char)的Keys.NumPad9)
{
button3_Click(NULL,NULL);
}
}
公共无效myControl_NumPad4(对象发件人,KeyPressEventArgs E)
{
如果(e.KeyChar ==(char)的Keys.NumPad4)
{
button4_Click(NULL,NULL);
}
}
公共无效myControl_NumPad5(对象发件人,KeyPressEventArgs E)
{
如果(e.KeyChar ==(char)的Keys.NumPad5)
{
button5_Click(NULL,NULL);
}
}
公共无效myControl_NumPad6(对象发件人,KeyPressEventArgs E)
{
如果(e.KeyChar ==(char)的Keys.NumPad6)
{
button6_Click(NULL,NULL);
}
}
公共无效myControl_NumPad1(对象发件人,KeyPressEventArgs E)
{
如果(e.KeyChar ==(char)的Keys.NumPad1)
{
button7_Click(NULL,NULL);
}
}
公共无效myControl_NumPad2(对象发件人,KeyPressEventArgs E)
{
如果(e.KeyChar ==(char)的Keys.NumPad2)
{
button8_Click(NULL,NULL);
}
}
公共无效myControl_NumPad3(对象发件人,KeyPressEventArgs E)
{
如果(e.KeyChar ==(char)的Keys.NumPad3)
{
button9_Click(NULL,NULL);
}
}


解决方案

修改结果
注意到,我必须更加清楚我是什么意思...



从你的代码贴我怀疑你有您添加关键事件9控制。当他们专注这些控件将只接收关键事件。



要在全球范围处理密钥的形式,你必须设置 Form.KeyPreview 真正。另外,我无法处理的按键你做的方式,但添加 Form.KeyDown 事件,写是这样的:

 开关(e.KeyCode)
{
情况下Keys.NumPad9:
e.Handled = TRUE;
button3.PerformClick();
中断;
情况下Keys.NumPad8:
e.Handled = TRUE;
button2.PerformClick();
中断;
//等等
}

这将处理数字小键盘,键在表单中 - 然后你可以删除所有你在你的问题张贴的事件处理程序



要编程点击的按钮,你应该使用 Button.PerformClick()方法中,作为一个以上的事件处理程序可以被添加到该点击事件,这将不会被否则称为



修改2 结果
开关语法语句来无效。当然,每个案例必须与情况关键字...现在它应该工作。


启动

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

解决方案

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.

这篇关于按键来模拟一个按钮单击C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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