Do the following:
Set the form's
KeyPreview
to true so that the form can capture the Spacebar.
this.KeyPreview = true;
Then use the KeyDown event of the form:
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
Suppose you have a button's event handler like this:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Button clicked");
}
Use the
button1.PerformClick()
in the Form1_KeyDown function:
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
button1.PerformClick();
}
}
Should work now.