Disable keyboard input

Hi all

Is there a way to disable SPACE when the focus is on a button so it does not actiavte it even when space is pressed

Also is there a way to disable the arrows keys so that it does not affect the tabcontrol when i press left/right key

Thankz



Answer this question

Disable keyboard input

  • jdrawmer

    Hi, Anthery

    About first question: press space key on button will raise the click event of it.

    This function is designed in ButtonBase class which Button derived from. In ButtonBase class there is method to process the keydown event.

    Here it is:

    protected override void OnKeyDown(KeyEventArgs kevent)
    {
       if (kevent.KeyData == Keys.Space)
       {
          ...
       }
       base.OnKeyDown(kevent);
    }

    Solution:

    Make a control derive the button control and override this method.

    Step 1: Create project from "Windows Control Library"

    Step 2: Create the control derived from Button

    Step 3: Override the OnKeyDown method

    Example code:

    public class CustomButton : Button
        {
            public UserControl1():base()
            { }
            protected override void OnKeyDown(KeyEventArgs kevent)
            {
                //base.OnKeyDown(kevent);
            }
        }

    Step 4: Compile the project, and a dll file is generated

    Step 5: Reference the dll file in your windows application as a userControl

                 ToolBox --> Choose Item--> Browse...

    Then you can use the Custom Button without raising the click event by pressing space key.

    Thank you



  • Disable keyboard input