Help with KeyDown or KeyPress

I am very new to VB but I am progressing well. However, I have tried numerous solutions to this problem and have not found the right answer yet:

I want my program to do something anytime a certain key is pressed. It is not going in a textbox or form. So Basically something like this:

If (The "5" Key is pressed) Then

DoTheseThings()

End If

I need it to always be active. Any help Thanks!!



Answer this question

Help with KeyDown or KeyPress

  • Rakosnicek

    Out of curiosity... Do you know what this warning means that is popping up now that I added that code Should I worry about it or can I fix it

    Warning 1 sub 'OnKeyPress' shadows an overridable method in the base class 'Control'. To override the base method, this method must be declared 'Overrides'.


  • TailsNZ

    If you mean a system wide solution, you need a global hook.

    http://www.codeproject.com/csharp/globalsystemhook.asp



  • hetnet

    That's it!!!! It works!!

    Thankyou!!


  • MightnMagic

    Go to the designer view, click on the form, and then click on the lightning flash in the properties. Go down to KeyPress and type in the name of this method (or hit the arrow and select it). Sorry, without this step, your code handles the method without telling the form that you intended this behaviour, it's working through OO, but without actually connecting to the event.



  • Plop

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

    TextBox1.Text = TextBox1.Text & e.KeyChar

    End Sub

    This works, but you also need to click on the form in the designer, and set the KeyPreview property to true.



  • RichardD

    Thank you... That may be what I need.

    For my first VB project (as a learning tool) I am building a calculator. The calculator works perfectly from a button clicking standpoint.

    Now I am trying to add keyboard input. And for complete functionality, I need it to work even if someone used the mouse to click the 4 and 5 buttons, and then switched to the 6 KEY to finish the number 456. My program is set up to handle this as long as I can read one key at a time and it can be read at any time (without focus on a textbox, i.e.)

    Is a global hook the best solution or is their an easier one using keypress or keydown


  • Old John

    I think that is what I am having the problem with... Don't I need a Textbox to catch the keypress What if the cursor is not in that textbox Will it still catch a keypress

    I tried that first, but it only seemed to work if I was typing into the textbox. If the focus was off the textbox, it wouldn't catch the keypress.


  • hotentote

    No, it's not what you need then. It's a complex solution. You just want to catch the KeyPress or KeyDown event in your app, and then append the key value to the string in the textbox.

    textBox1.Text = textBox1.Text & theChar

    assuming that theChar contains the character pressed, and assuming that textBox1 is your textbox. You can use char.IsDigit to make sure it's a number first.



  • psycheval

    i guess using the ascii value of keys will help



  • cjjrmj

    OK - I see the problem. If you have a textbox, the keypress is caught for you. If your entire form has a keypress event, then you have to manually work out what key was pressed, and put that key on the end of the string. Or, you could just call the textboxes keypress event with the event args that come into your handler for the whole form



  • wyeechen

    It actually filled it in on it's own. It is working just fine. It just shows a "Warning" on my warning list.

    I greatly appreciate the help... It will work for all needed keys(numbers and +, -, *, etc.)

    Just not the "Enter" key. That will be my next challenge to solve.


  • Kiran Patil

    Yes! Excellent... I think that is exactly what is wrong and it is what I don't how to do:

    How do I put a keypress event on the entire form

    I can figure out how to process the input once I capture it... I just haven't been able to figure out how to capture it without a textbox.

    Thanks for your patience! I am definately a noob to VB.


  • Help with KeyDown or KeyPress