Key down event affected by buttons

*爱你&永不变心* 提交于 2019-12-12 15:36:36

问题


I'm new around here and i have a little problems with a C# application. I want to capture the key down event. This wasn't a problem at first but after i added some buttons to the form, the key down event of the form ignores the arrow keys and moves the focus from one button to the next.(The key up event works) Is there a way to stop this and make them do something else when i hold the arrow keys?


回答1:


Set the KeyPreview property on the Form to true. That will allow the form to see the keydown event in addition to the child controls.

Add this to your Form ...

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData.Equals(Keys.Right))
    {
        MessageBox.Show("Right Key Pressed!");
    }

    return base.ProcessCmdKey(ref msg, keyData);
}



回答2:


If you don't want the normal key down functionality for the controls you will need to set the key down event on each control, and set the handled attribute for the event arguments to be true, that way it doesn't bubble up to the built in control functionality.



来源:https://stackoverflow.com/questions/818092/key-down-event-affected-by-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!