C# Interception of user input/ Restrict user input via keyboard

131 Views Asked by At

I have the problem, that i like to programm a ui in windows forms. there i only want to allow user input through the buttons from the ui, which i already have ready, and number input through the keys on the keyboard. All other keys should not work. How can I do this?

1

There are 1 best solutions below

0
On

Handle the KeyDown event:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    e.Handled = true;

    // Top row of numbers or keypad
    if ((e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9) || (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9))
    {
        // Do something with the number
    }
}