简体   繁体   中英

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

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?

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM