Results 1 to 10 of 10

Thread: [RESOLVED] function to capture keyboard events. Where do I find that?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    212

    Resolved [RESOLVED] function to capture keyboard events. Where do I find that?

    I have a Form project with several combobox's on it. I need to catch the <ENTER> key.

    How do I get the function generated, or do I need to create that myself?

    EDIT: In VBA you could select the function of an item from a dropdown box and it would generate the code. Anything like this in VB Net?
    Last edited by FunkMonkey; Mar 21st, 2025 at 07:53 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,017

    Re: function to capture keyboard events. Where do I find that?

    What do you want to do when your user presses Enter?

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,817

    Re: function to capture keyboard events. Where do I find that?

    There's still a drop down. At the top of the code page, there should be three different dropdowns. The left one is for project, which you can ignore because you have only one. The middle one has all the different objects on the form that have events, which includes all the controls. Select the combobox that you want to deal with in there. The rightmost dropdown has all the different events.

    The Enter key seems unlikely to be relevant to a combobox. Most likely, you want the SelectedIndexChanged event, or something like that.
    My usual boring signature: Nothing

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,076

    Re: function to capture keyboard events. Where do I find that?

    The Navigation Bar at the top of the code window that Shaggy is referring to will allow you to generate an event handler for any field declared WithEvents, which is what's required for that field to be included in the Handles clause of such a method. When you add a control or component in the designer, a field is generated with the Name property and declared WithEvents. You can see these fields if you open the designer code file for the form. Note that you can also declare a field WithEvents in your own code and it will then appear in the Navigation Bar.

    WithEvents and Handles don't exist in C#, so you can't generate event handlers that way. In C#, you have to use the Properties window in the designer to automatically generate an event handler, and you can do the same thing in VB. In the designer, select the control or component of interest, open the Properties window, click the Events button and have at it. You can double-click an event to generate a new event handler or you can select an existing event handler from the drop-down list. This is handy for attaching the same method to the events of multiple controls, even if the controls are not the same type or even for the same event. You can also select multiple controls in the designer and then use the properties window to generate or attach an event handler to all of them at the same time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    212

    Re: function to capture keyboard events. Where do I find that?

    Quote Originally Posted by Shaggy Hiker View Post
    There's still a drop down. At the top of the code page, there should be three different dropdowns. The left one is for project, which you can ignore because you have only one. The middle one has all the different objects on the form that have events, which includes all the controls. Select the combobox that you want to deal with in there. The rightmost dropdown has all the different events.

    The Enter key seems unlikely to be relevant to a combobox. Most likely, you want the SelectedIndexChanged event, or something like that.
    Thanks Shaggy, but I really do wan to process the <ENTER> key. This is possible in VBA.

    Code:
    Private Sub CB_EVENT_NUMBER_KeyDown(ByVal KeyCode As msforms.ReturnInteger, ByVal Shift As Integer)
    
        Dim szMenuSelection As String
        Dim nMenu As Integer
        
        If KeyCode = vbKeyReturn Then
            'clear our flag
            AbortRun = False
            nMenu = GetMenuEnumFromRunOption(RunOption)
            szMenuSelection = GetMenuSearchElementFromMenuEnum(nMenu, RunOption)
            DoEvents
            Call CBeventNumberExecute(szMenuSelection, CB_EVENT_NUMBER.Text, RunOption)
            DoEvents
            
        End If
        
    End Sub
    I need to catch when the user presses the enter key, I ensure it is it and then I process a routing to start the process for the program.

    Is there any way to create one if VB does not offer something similar?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    212

    Re: function to capture keyboard events. Where do I find that?

    Quote Originally Posted by jmcilhinney View Post
    The Navigation Bar at the top of the code window that Shaggy is referring to will allow you to generate an event handler for any field declared WithEvents, which is what's required for that field to be included in the Handles clause of such a method. When you add a control or component in the designer, a field is generated with the Name property and declared WithEvents. You can see these fields if you open the designer code file for the form. Note that you can also declare a field WithEvents in your own code and it will then appear in the Navigation Bar.

    WithEvents and Handles don't exist in C#, so you can't generate event handlers that way. In C#, you have to use the Properties window in the designer to automatically generate an event handler, and you can do the same thing in VB. In the designer, select the control or component of interest, open the Properties window, click the Events button and have at it. You can double-click an event to generate a new event handler or you can select an existing event handler from the drop-down list. This is handy for attaching the same method to the events of multiple controls, even if the controls are not the same type or even for the same event. You can also select multiple controls in the designer and then use the properties window to generate or attach an event handler to all of them at the same time.
    Thanks JMC, I'll give this a try and see if I can get it working. Maybe Shaggy has a way as well.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    212

    Re: function to capture keyboard events. Where do I find that?

    Quote Originally Posted by Shaggy Hiker View Post
    There's still a drop down. At the top of the code page, there should be three different dropdowns. The left one is for project, which you can ignore because you have only one. The middle one has all the different objects on the form that have events, which includes all the controls. Select the combobox that you want to deal with in there. The rightmost dropdown has all the different events.

    The Enter key seems unlikely to be relevant to a combobox. Most likely, you want the SelectedIndexChanged event, or something like that.
    Thanks Shaggy, but I really do need to capture the <ENTER> key. Here is my VBA code.

    Code:
        Private Sub CB_EVENT_NUMBER_KeyDown(ByVal KeyCode As msforms.ReturnInteger, ByVal Shift As Integer)
    
            Dim szMenuSelection As String
            Dim nMenu As Integer
    
            If KeyCode = Keys.Return Then
                'clear our flag
                SetAbortRun(False)
                nMenu = GetMenuEnumFromGetRunOption()(GetRunOption())
                szMenuSelection = GetMenuSearchElementFromMenuEnum(nMenu, GetRunOption())
    
                CBeventNumberExecute(szMenuSelection, CB_EVENT_NUMBER.Text, GetRunOption())
    
    
            End If
    
        End Sub
    The user needs to press enter in the combobox to initiate the event. It's not a change of the index, but the <ENTER> key which fires the process of the program.

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,382

    Re: function to capture keyboard events. Where do I find that?

    Quote Originally Posted by FunkMonkey View Post
    Thanks Shaggy, but I really do need to capture the <ENTER> key. Here is my VBA code.

    Code:
        Private Sub CB_EVENT_NUMBER_KeyDown(ByVal KeyCode As msforms.ReturnInteger, ByVal Shift As Integer)
    
            Dim szMenuSelection As String
            Dim nMenu As Integer
    
            If KeyCode = Keys.Return Then
                'clear our flag
                SetAbortRun(False)
                nMenu = GetMenuEnumFromGetRunOption()(GetRunOption())
                szMenuSelection = GetMenuSearchElementFromMenuEnum(nMenu, GetRunOption())
    
                CBeventNumberExecute(szMenuSelection, CB_EVENT_NUMBER.Text, GetRunOption())
    
    
            End If
    
        End Sub
    The user needs to press enter in the combobox to initiate the event. It's not a change of the index, but the <ENTER> key which fires the process of the program.
    Here you would use the "e" parameter to check what key was pressed.

    Code:
        Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Return Then
                MessageBox.Show("Enter key pressed")
            End If
        End Sub

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    212

    Re: function to capture keyboard events. Where do I find that?

    Quote Originally Posted by jmcilhinney View Post
    The Navigation Bar at the top of the code window that Shaggy is referring to will allow you to generate an event handler for any field declared WithEvents, which is what's required for that field to be included in the Handles clause of such a method. When you add a control or component in the designer, a field is generated with the Name property and declared WithEvents. You can see these fields if you open the designer code file for the form. Note that you can also declare a field WithEvents in your own code and it will then appear in the Navigation Bar.

    WithEvents and Handles don't exist in C#, so you can't generate event handlers that way. In C#, you have to use the Properties window in the designer to automatically generate an event handler, and you can do the same thing in VB. In the designer, select the control or component of interest, open the Properties window, click the Events button and have at it. You can double-click an event to generate a new event handler or you can select an existing event handler from the drop-down list. This is handy for attaching the same method to the events of multiple controls, even if the controls are not the same type or even for the same event. You can also select multiple controls in the designer and then use the properties window to generate or attach an event handler to all of them at the same time.
    JMC, I tried to select the object -> open Properties, but in Properties is no place to select other handlers. I would show a picture but my screen captures do not wan to load. They look like they are uploading but nothing actually uploads, so I can't show a picture of where/what I'm doing. But the start of the comment indicates what I am doing.

    Am I missing something?

    EDIT: OK...... I found the handlers. I was looking in the wrong place. Thanks JMC.
    Last edited by FunkMonkey; Mar 22nd, 2025 at 03:58 PM.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    212

    Re: function to capture keyboard events. Where do I find that?

    Quote Originally Posted by wes4dbt View Post
    Here you would use the "e" parameter to check what key was pressed.

    Code:
        Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Return Then
                MessageBox.Show("Enter key pressed")
            End If
        End Sub
    Thanks wes, looks like what I need.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width