用户控件的KeyDown事件中,不接收方向键,其它键都有反应,请各位指点一下。
KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show(e.KeyCode.ToString());
}
当我按上下左右方向键的时候没反应,其它键没问题

解决方案 »

  1.   


    不行,程序不接收Keys.Down,Keys.Up,Keys.Left,Keys.Right,
      

  2.   

    MSDN上有明确的说明:
    Certain keys, such as the TAB, RETURN, ESCAPE, and arrow keys are handled by controls automatically. To have these keys raise the KeyDown event, you must override the IsInputKey method in each control on your form. The code for the override of the IsInputKey would need to determine if one of the special keys is pressed and return a value of true.要处理这些键的话override基类的IsInputKey方法即可。
      

  3.   

    当然,直接override基类的WndProc方法来截获Windows消息应该也可以,但是重写IsInputKey应该更直观些吧。
      

  4.   

    程序建一个主FORM,一个用户控件UserControl1,主FORM里有一个panel1,在主FORM的Load事件里
    UserControl1 user1= new UserControl1();
    panel1.Controls.Add(user1);
    panel1.Controls[0].Focus();在用户控件UserControl1里
    private void UserControl1_KeyDown(object sender, KeyEventArgs e)
            {
                MessageBox.Show(e.KeyCode.ToString());
    }
    运行后,按方向键不弹出任何提示
      

  5.   

    protected override bool ProcessDialogKey(Keys keyData)
            {
                if (keyData == Keys.Escape)
                {
                    Cursor.Current = Cursors.Default;
                    Cursor.Show();
                    this.Close();
                    return true;
                }
                return false;
            }