tags:

views:

38

answers:

2

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?

A: 

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.

Paul Sasik
Still doesn't seem to work. If any control is selected, it won't execute there. I've also tried adding it to the list view, and it still just makes the beeping sound :/
Mike
Try this: http://www.codeproject.com/KB/system/globalmousekeyboardlib.aspx
Paul Sasik
A: 

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?

RichAmberale