none
How to capture keypress in Windows Forms?

    Question

  • Hi every body

    How can i capture key press in Windows Forms not from a Control?
    I want the user to press any key any where in the Form and display the keypress in label
    like if you write in the text box


    I knew that i have to setup  (  KeyPreview  to true in the Form1 ) and use

            private void Form1_KeyUp(object sender, KeyEventArgs e)
            {

             }

    to capture the key press.

    But my problem is that i did not get the value correctly in the label

    Pressing number 5 in keyboard will give
    1. If using e.KeyCode
    ===========================
    label1.Text = e.KeyCode.ToString();

    NumKey will give  ===>>  NumPad5
    MainKey will give  ===>>   D5

    2. If using e.KeyValue
    ==========================
     label1.Text = e.KeyValue.ToString();

    NumKey will give  ===>>101
    MainKey will give  ===>>53

    3. If using e.KeyData
    ==========================
    label1.Text = e.KeyData.ToString();

    NumKey will give  ===>>  NumPad5
    MainKey will give  ===>>   D5

    Any help please
    Thursday, January 11, 2007 3:17 PM

Answers

  • This worked fine, even when the focus was on a TextBox:

        private void Form1_KeyPress(object sender, KeyPressEventArgs e) {
          label1.Text += e.KeyChar.ToString();
        }

    Thursday, January 11, 2007 10:14 PM

All replies

  • How about somthing like

    label1.Text = (char)e.KeyValue;

    Thursday, January 11, 2007 4:53 PM

  • same thing
    Pressing number 5 in keyboard will give


    label1.Text = ((char)e.KeyValue).ToString();

    NumKey will give  ===>>  e    (Wrong)
    MainKey will give  ===>>   5  this one is ok

    Thursday, January 11, 2007 5:08 PM
  • Every key on your keyboard has a key number.  KeyEventArgs.KeyValue gives you that key number.  The key number for the key that produces "5" just happens to match the ASCII value of the character "5" (it wasn't a coincidence).  The key number for key "5" on the numeric pad is of course different.  It is key #101 as you found out.

    To get the kind of values you're looking for, you'll need to use the KeyPress event.  Its KeyPressEventArgs.KeyChar will give you the proper character code for both "5" and NumPad5.  Beware of the NumLock key.
    Thursday, January 11, 2007 8:36 PM
  • Thanks nobugz

    But the KeyPressEvent will not capture the key on the hall Form

    Would you please give me small example if you have time

    Thanks
    Thursday, January 11, 2007 9:03 PM
  • This worked fine, even when the focus was on a TextBox:

        private void Form1_KeyPress(object sender, KeyPressEventArgs e) {
          label1.Text += e.KeyChar.ToString();
        }

    Thursday, January 11, 2007 10:14 PM
  •  nobugz wrote:
    This worked fine, even when the focus was on a TextBox:

    private void Form1_KeyPress(object sender, KeyPressEventArgs e) {
    label1.Text += e.KeyChar.ToString();
    }



    Thanks nobugz

    This solve my problem. It works even when i focus on any control ....

    Kind Regards
    Thursday, January 11, 2007 10:33 PM
  • i do not seem to get the system to interupt at all...

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    My.Computer.Audio.Play("c:\workspace\honey.mp3.mp3", AudioPlayMode.WaitToComplete)

    End Sub

    Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

    MessageBox.Show("you tapped a key")

    End Sub

    Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

    MessageBox.Show("key down")

    End Sub

    my eventual intent is to get the computer to respond to me when a online video is playing full screen.  right now i have to wait until it completes.

     

    a

    Sunday, January 14, 2007 11:18 PM
  •  

    Hey all, I can't seem to get keypress event to fire correctly... I don't know whats up.

    This is what I've got.

     

    Code Block

    this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(Form1_KeyPress);

     
    And then I have this:
     

    Code Block

    private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

    {

    MessageBox.Show(e.KeyChar.ToString());

    }

     

     

    Whats up? Nothing happens when I press any key anywhere on my form. In control or out. Just doesn't want to work.

     

    Thanks in advance.

    Monday, January 07, 2008 4:39 PM
  • Set the form's KeyPreview property to TRUE

     

    Monday, January 07, 2008 4:47 PM
  • Cool thanks that worked. At first I thought it didn't but I had to close and open visual studio to get it to work.

     

    I notice this does not detect when the arrow keys are pressed, is there something else that I need to use for that? KeyDown or KeyUp maybe?

     

    Thanks again.

    Monday, January 07, 2008 4:52 PM
  •  Sherry Coke wrote:

    Cool thanks that worked. At first I thought it didn't but I had to close and open visual studio to get it to work.

     

    I notice this does not detect when the arrow keys are pressed, is there something else that I need to use for that? KeyDown or KeyUp maybe?

     

    Thanks again.

     

    KeyDown will work. This showed me the arrow keys fine.

     

    private void Form1_KeyDown(object sender, KeyEventArgs e)

    {

        MessageBox.Show(e.KeyCode.ToString());

    }

     

    Monday, January 07, 2008 5:41 PM
  • apologize.. off topic. Sherry, if you know shadowman.. contact asap. important

     

     

     

    Saturday, April 05, 2008 5:35 AM