Search code examples
c#visual-studiokeyboardkeydetect

Detect ANY Key Press and Save


I know in Visual Studio you can detect a specific key press (EX: Here), but is there a way to detect any key (A-Z, 0-9, Shift, Ctrl, Alt, F1, F2 etc) and display it as a label (EX: label1). I intend to be able to use this while in any windows, not just the Visual Studio Program that i will be making. THANKS!


Solution

  • Make condition according to your need. Example Source Here

    void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar >= 48 && e.KeyChar <= 57)
        {
            MessageBox.Show("Form.KeyPress: '" +
                e.KeyChar.ToString() + "' pressed.");
    
            switch (e.KeyChar)
            {
                case (char)49:
                case (char)52:
                case (char)55:
                    MessageBox.Show("Form.KeyPress: '" +
                        e.KeyChar.ToString() + "' consumed.");
                    e.Handled = true;
                    break;
            }
        }
    }