Key pressed

Hi,

I know how to check if a single key has been pressed, as long as it is an ASCII character. But how can I check for, say, the Insert key, or a combination like Control-X

Regards,

  Guido


Answer this question

Key pressed

  • Dhatrii

    If there is, I don't know about it.

  • DORANGE

    It's not a difficult concept, it's just tedious to implement. Have you read the help topics

    Here's some sample code from Help:



    private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        // Determine whether the key entered is the F1 key. If it is, display Help.
        if(e.KeyCode == Keys.F1 && (e.Alt || e.Control || e.Shift))
        {
            // Display a pop-up Help topic to assist the user.
            Help.ShowPopup(textBox1, "Enter your name.", new Point(textBox1.Bottom, textBox1.Right));
        }
        else if(e.KeyCode == Keys.F2 && e.Modifiers == Keys.Alt)
        {
            // Display a pop-up Help topic to provide additional assistance to the user.
            Help.ShowPopup(textBox1, "Enter your first name followed by your last name. Middle name is optional.",
                new Point(textBox1.Top, this.textBox1.Left));
        }
    }

     


  • Randa Maher

    Is there a table somewhere or is every programmer forced to test all the keys him/herself

    If Control-X is pressed, does KeyPressed also notice the Control key, or just the X

    Regards,

      Guido


  • foxquote

    What are you trying to accomplish   If you're trying to get shortcut/mnemonic behavior for a toolstrip menu, for example, there are better ways of doing this.

  • durayakar

    Thanks, Bonnie!

    Is there a way to distinguish between left and right Shift/Alt/Ctrl

    Regards,

      Guido

  • Vishal Kathuria

    Using the KeyDown event you can capture these keys:


    private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)

    {

        Debug.WriteLine(e.KeyCode);

    }


     


    The Ctrl, Alt, Shift keys are members of the KeyEventArgs parameter.



  • Endren

    I want to take action, like changing the camera position, if the user presses, for instance, Shift-PageUp.
    Therefore, I must be able to detect it when the user does that.

    I never imagined this to be such a difficult concept ...

  • Key pressed