C#_ Making buttons be pressed by either mouse or keyboard

房东的猫 提交于 2021-01-28 05:41:57

问题


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.


回答1:


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

}



回答2:


You need an event handler to tie to your method "AnyNumberClick". This is done in the Designer.cs portion of your form. Create a character array char[] and create a function within a button pressed event method, and then compare the button pressed against the set of characters in your array.

private void txt_box_keypress(object sender, KeyPressEventArgs e)
    {
    char[] SomeArray = {'a','b','c', etc};
    int LengthOfArray = SomeArray.Length;

for (int x = 0; x < LengthOfArray; x++)
    {
    if (txt_box.Text.Contains(SomeArray[x]))
    {
    'Your method event here'
    }
  }
}


来源:https://stackoverflow.com/questions/63090062/c-making-buttons-be-pressed-by-either-mouse-or-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!