C#_通过鼠标或键盘按下按钮 [英] C#_ Making buttons be pressed by either mouse or keyboard

查看:287
本文介绍了C#_通过鼠标或键盘按下按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以在我的程序中,我尝试制作按钮并为每个按下的按钮分配不同的方法.但是我遇到了一个问题,我还希望用户使用他的键盘并将键盘上按下的按钮分配给屏幕上的相同按钮.但是首先,我尝试用鼠标或键盘按下按钮,但是该方法不允许'EventArgs'中的KeyEvents(这对我来说很好),所以我创建了一个不同的方法并创建了一个布尔变量,以便在该单独的方法中按下键,使该变量为true,然后在该主方法中将该变量为true,然后执行代码,但是程序忽略了该键盘变量,我不知道为什么.

Ok, so in my program I tried making buttons and assigning different methods for each button pressed. But I came into a problem where I also want the user to use his keyboard and assign buttons pressed on keyboard into same buttons on screen. Yet firstly, I tried if button is pressed by mouse or keyboard yet the method doesn't allow KeyEvents in 'EventArgs' (which is fine by me), so I created different method and made a boolean variable so that if in that separate method the key is pressed, make that variable true and in that main method if that is true then perform the code, yet the program ignores that keyboard variable and I have no idea why.

然后我尝试开一门不同的课,因为我认为这可能会有所帮助.现在,我可以在其中调用该类和方法,但是不能传递参数,因为它说这是一个方法,因此它不能做其他任何事情,只能被调用.

Then I tried making a different class as I thought maybe that would help. Now I can call that class and method inside it but not pass a parameter as it says it's a method so it can't do anything else but only be called.

如果您好奇的话,下面是下面的代码...

If you're curious, here's the code below...

___
// the button '1' variable
bool pressOne = false;

___
// method for if that button is pressed
private void AnyNumberClick(object sender, EventArgs e)
{
   Button btnSender = (Button)sender;

   if (btnSender == btn_Num1 || pressOne)
   {
      // if button is pressed by either, perform code   
   }
}

___
// method for detecting which key is pressed for certain bool variable into button's method
public void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.D1)
            {
                pressOne = true;
            }
            else
            {
                pressOne = false;
            }

___
// Call another class inside 'From1_KeyDown' method

Class1 newclass = new Class1();

newclass.buttonused();

NumResult.Text = newclass.buttonused.num();

一个我不知道该如何上课的人.我什至不知道新班级对我是否有帮助.我做了研究,但没有找到答案.感谢您的帮助.

The one with class I don't know how to start it. I don't even know if new class will help me there or not. I did the research but didn't find the answer. I appreciate any help from this.

推荐答案

以这种方式尝试.我设置了Dictionary<Keys, Button>来表示键和按钮之间的关系.然后,我重写了ProcessCmdKey()来捕获按键.如果按下的键存在于我们的查找中,则使用.PerformClick():

Try it this way. I've setup a Dictionary<Keys, Button> to represent the relationship between a Key and a Button. Then I've overridden ProcessCmdKey() to trap key presses. If the key pressed exists in our lookup, then we click it with .PerformClick():

public partial class Form1 : Form
{

    private Dictionary<Keys, Button> btnLookups = new Dictionary<Keys, Button>();

    public Form1()
    {
        InitializeComponent();

        // make your key -> button assignments in here
        btnLookups.Add(Keys.F1, button1); 
        btnLookups.Add(Keys.F2, button2);
        btnLookups.Add(Keys.F3, button3);
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        Button btn;
        if (btnLookups.TryGetValue(keyData, out btn))
        {
            btn.PerformClick();
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("button1");
    }

    private void button2_Click(object sender, EventArgs e)
    {
        MessageBox.Show("button2");
    }

    private void button3_Click(object sender, EventArgs e)
    {
        MessageBox.Show("button3");
    }

}

这篇关于C#_通过鼠标或键盘按下按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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