I'm trying to make it so no matter what, when I push Space, a certain block of code is executed (cmd_play, to be exact).
However, it only seems to work once if you do it using Form Keypress:
private void frmmain_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToChar(Keys.Space))
cmdPlay_Click(null, null);
开发者_开发知识库 }
any ideas?
Try to set the Handled property of KeyPressEventArgs to true. Not sure if it'll fix your issue but it is good form. More info here
private void frmmain_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToChar(Keys.Space))
{
cmdPlay_Click(null, null);
e.Handled = true;
}
}
If that doesn't work then keyboard event hooks definitely will. Though hooking events is a much larger and more dangerous undertaking.
Are you sure that cmdPlay_Click() isn't the problem? I.E. the event handler IS getting called multiple times but cmdPlay_Click() is only playing once?
精彩评论