开发者

Override Keybindings

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜