Results 1 to 4 of 4

Thread: NEWBIE ALERT: Event Handling

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    16

    NEWBIE ALERT: Event Handling

    I have alot of experience in working in VBA for excel, but not so much experience in creating my own structures so it's all still a bit foggy.

    I'm trying to capture the event for when the F1 key is pressed, not just when the form is active, but anytime the program is running. The code below expressing the handling is automatically generated for me in VB Express... But I'd like to change the situation to encompass anytime the program is open. I guess I have to reference events that happen in windows in general, but i don't know how to do it. I think i have to create the even in a class module??? If someone could kind of shove me in the right direction, I'd be very much appreciative.

    Code:
     Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = "112" Then
                Call SaveWholeForm()
                
            End If
        End Sub

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

    Re: NEWBIE ALERT: Event Handling

    search the forum for registerhotkey/unregisterhotkey api's to use system wide hotkeys. i wouldn't recommend using the F1 key though. it's used as a help key by windows

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: NEWBIE ALERT: Event Handling

    1. You need to set the form.KeyPreview property to True. In form designer, you click on the form to select it and look in the properties window for the KeyPreview property and change it to True (default value is False)
    2. Paste in this code
    Code:
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = Keys.F1 Then
                
               SaveWholeForm()
    
            End If
        End Sub
    Edit: Never mind... I didn't know that you need a system wide keyboard hook
    Last edited by stanav; Feb 15th, 2011 at 04:45 PM. Reason: Never mind... I didn't know that you need a system wide keyboard hook
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

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

    Re: NEWBIE ALERT: Event Handling

    It is a very bad idea to use F1 as a hotkey. That is the standard Windows shortcut for Help, so users might want to open Help in some other app and they get your app doing something else. Use a hotkey that is unlikely to be in use elsewhere and always use at least two modifier keys.
    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

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