我自己做了一个usercontrol,添加了一个基于usercontrol的键盘事件,但是在form 中使用这个控件的时候却键盘事件,这是为什么?求各位帮忙解答

解决方案 »

  1.   

    但是在form 中使用这个控件的时候却接收不了键盘事件
      

  2.   

    控件里必须设置为可以接受焦点
    SetStyle(System.Windows.Forms.ControlStyles.Selectable,true)
      

  3.   

    你在keydown里面什么事都不作,只加个MessageBox.Show(e.keycode);
      

  4.   

    我在keydown上设了断点,问题是在按下方向键后进不去
      

  5.   

    protected override bool IsInputKey(Keys keyData) {
    2.
    /* 這裡需回傳true, 否則無法接收到方向鍵
    3.
    * MSDN:
    4.
    * Call the IsInputKey method to determine whether the key specified by the keyData
    5.
    * parameter is an input key that the control wants.
    6.
    * This method is called during window message preprocessing to determine whether
    7.
    * the specified input key should be preprocessed or sent directly to the control.
    8.
    * If IsInputKey returns true, the specified key is sent directly to the control.
    9.
    * If IsInputKey returns false, the specified key is preprocessed and only sent to
    10.
    * the control if it is not consumed by the preprocessing phase.
    11.
    * Keys that are preprocessed include the TAB, RETURN, ESCAPE, and the UP ARROW,
    12.
    * DOWN ARROW, LEFT ARROW, and RIGHT ARROW keys. */
    13.
    return true;
    14.
    }
      

  6.   

    通过重写ProcessCmdKey  应该可以吧