让UserControl处理Arrow key

Posted on 2005-08-29 13:21  Vitoria  阅读(529)  评论(0编辑  收藏

前阵子写了个UserControl, 需要处理Arrow keys.( Up, Down, Left, Right). 但是这四个键属于DialogKey,有两种解决方案可以使得UserControlOnKeyDown中处理这四个键。

方案一:

重载ProcessDialogKey

protected override bool ProcessDialogKey(Keys keyData)

{

    if (keyData == Keys.Up || keyData == Keys.Down

          || keyData == Keys.Left || keyData == Keys.Right)

         return false;

     else

         return base.ProcessDialogKey(keyData);

}

 

方案二:

重载ProcessKeyPreview,强制加上Key消息事件分发的逻辑;然后重载IsInputKey    

protected override bool ProcessKeyPreview(ref Message m)

{

    if (base.ProcessKeyEventArgs(ref m))

    {

        return true;

     }

     return base.ProcessKeyPreview (ref m);

 }

protected override bool IsInputKey(Keys keyData)

{

    if ( this.Focused )

    {

        Keys keys1 = (keyData & Keys.KeyCode);

        switch (keys1)

        {

            case Keys.Up:

            case Keys.Down:

            case Keys.Left:

            case Keys.Right:

            {

                 return true;

             }

         }

      }

      return base.IsInputKey(keyData);

  }

随笔 - 19, 文章 - 0, 评论 - 59

Copyright © 2020 Vitoria
Powered by .NET 5.0.0 on Kubernetes