Keypress To Simulate A Button Click in C#
Solution 1
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.
Solution 2
You need to use button1.PerformClick();
for every button to invoke event correctly, here's info.
Comments
-
Cistoran almost 4 years
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); } }
-
Cistoran over 14 yearsThis seems to be what I'm looking for but I can't figure out where to put your code. I set KeyPreview = true; when Form1 initializes after that I just get stuck.
-
Thorsten Dittmar over 14 yearsJust set it in the form's properties (Properties window). In the Properties window change the view to the events-section, look for the KeyDown-event and double click that item. This adds a new event Form_KeyDown to your code, where you put my code. Alternatively, go to the form's source code and type "override" - from the Code Insight window, select the "OnKeyDown" method. A stub is generated for you. After the "base.OnKeyDown" line, add my code.
-
Cistoran over 14 yearsI just tried that and I'm afraid it still doesn't work. I get a couple errors dwindled down from when I replaced the : with ; The main ones being. No enclosing loop of which to break or continue(these are from break;) Only assignment, call, increment, decrement, and new object expressions can be used as a statement. (x3) (I get this on the Keys.NumPad and on the closing bracket of switch.
-
Thorsten Dittmar over 14 yearsOops, sorry, wrong syntax for the switch statement in my sample. I'll update my reply.