none
VB.NET Keydown event is not firing RRS feed

  • Question

  • Hi,

    I am trying to use the Keydown event of a VB.NET form, but its not working.

    When I say "not working", I mean the event isnt firing. I have put a breakpoint at the start of the event, and it isnt tripping up.

    What would prevent the Keydown event from firing?
    Wednesday, August 1, 2007 10:14 AM

Answers

All replies

  • Is the "KeyPreview" Property of the form set to true?

     

    Wednesday, August 1, 2007 10:21 AM
  • Ah ha - no it wasnt. But it is now. Thanks.
    Wednesday, August 1, 2007 10:24 AM
  • Hi,

     

    i got the same problem and implemented the same solution like "KeyPreview" property of the form to "True".

     

    this is working if the form is empty(i.e with blank form with out controls). 

     

    But if i place two button controls on the winform

     and tried to capture the keyPress event by clicking on right arrow key on the key borard

    i found just the focus is changing from one button to another but the KeyPress event is not firing.

     

    Please suggest me.

    Need to capture keypress event in VB.NET regardless as to what has focus.

     

     

     

     

    Wednesday, September 10, 2008 6:25 AM
  •  Karthikeya Pavan Kumar .B wrote:
    Is the "KeyPreview" Property of the form set to true?

     

     

    Hi karthikeya,

     

    i got the same problem and implemented the same solution like "KeyPreview" property of the form to "True".

     

    this is working if the form is empty(i.e with blank form with out controls). 

     

    But if i place two button controls on the winform

     and tried to capture the keyPress event by clicking on right arrow key on the key borard

    i found just the focus is changing from one button to another but the KeyPress event is not firing.

     

    Please suggest me.

    Need to capture keypress event in VB.NET regardless as to what has focus.

     

    Wednesday, September 10, 2008 6:26 AM
  • Gee, this is an old posting your a replying to. But I'll take a stab, even though I ended up doing my project in C#. I hope you dont mind I include the possible solution as C# - I think VB.NET and C# are kind of similarish anyway.

     

    What I think you need to do is add the code to the keydown event of every single control. I can see two ways of doing this - before the controls are added, or after.

     

    If you try to do this before the controls are added, do this:

    Add a "ControlAdded" event to the form. This should be fired as new controls are added. In this event, add some code like:

    e.Control.KeyPress += new KeyPressEventHandler(Control_KeyPress);

     

    This is C#, so VB.NET will obviously be a bit different. This way you can have all the controls calling the "Control_KeyPress" routine when a key is pressed.

     

    Another option is to do something similar, but to do it after the controls have been added. For this you will need to, after all the controls have been loaded, cycle through all the controls, and add the KeyPress event to them all (all calling the same code of course).

     

    I hope this has helped, although I suspect it might have confused you...

    Wednesday, September 10, 2008 6:44 AM
  • Hi fweeee,

     

    Thanks for your reply.

     

    What you explained is very clear.

     

    but the problem is, when i click on keyboard arrow key the key press event is not firing for any control, only the control focus is changing. no event is firing.

     

    my requirement is,form keypress event should fire for keyboard right arrow key press.

     

     Thanks.

     

    Thursday, September 11, 2008 10:40 AM
  • As I found this post, and because it didn't give the solution for me, I had to think about the solution myself (Google before you think), I write here my findings so that the next one can hopefully benefit from it.

    True that at least the KeyPreview-property on the form has to be set, if not only key-events on the form itself will be handled by the eventhandler of the form.

    In case 'Enter' is the key that doesn't trigger the event, check if you haven't any AcceptButton set on the form : even when this button is disabled, it will eat up the Enter-event. Same goes for the CancelButton and the escape-key.

    In case the Enter-key needs to be used to go to the next field (ie enter data and confirm with Enter, plus skipping to the next field), a more controlable approach is the following as you can selectivly add controls that should react or not, and even react diffently depending the [type of] control:

    Private Sub data_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles _
            tbx.KeyDown, cbx.KeyDown, chk.KeyDown, ....

             If e.KeyCode = Keys.Return Then
                e.SuppressKeyPress = True
                SendKeys.Send("{Tab}")
            End If

    End Sub

    One could write it in de form_keydown-event (with KeyPreview = True), but then one should exclude all objects that should not have the behaviour selectivly

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

             'may need some extra checking on nothing....
             If e.KeyCode = Keys.Return andalso sender.ActiveControl is not btn Then
                e.SuppressKeyPress = True
                SendKeys.Send("{Tab}")
            End If

    End Sub


    Friday, January 7, 2011 2:07 PM
  • Another reason this will happen.. is if the textbox is multiline. You then have to set the AcceptsReturn property to true on the textbox to get the even to fire.

    Just saying..

    Friday, February 24, 2012 6:48 AM
  • hi

    i had same problem before 

    the problem was in  accept button  i fix it by  changed to (none)

    and it's  will  be work  

    i hope this will help u

    Monday, September 16, 2013 9:44 PM