Results 1 to 7 of 7

Thread: [RESOLVED] Universal Key Press

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Resolved [RESOLVED] Universal Key Press

    How do I go about making this code work no matter what form is present:

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        If KeyCode = vbKeyF1 Then
            Config.Show
        End If
    End Sub
    I have about 30 forms so I don't want to have to add something to every form, is there a way that that code can be modified so that no matter what form is on screen, if I press F1 it will do the command?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Universal Key Press

    hmmm, no matter which control on any form has the focus? Brainteaser, a registered hotkey probably wouldn't be a good solution since other apps use F1 for other purposes. Adding a keyboard hook would work, but can be far more hassle than updating the 30 forms. Thinking...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Universal Key Press

    Oh, doesnt have to be F1.. I just used that as an example, bad example; sorry. It can be and F row button..

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Universal Key Press

    Regarding a hotkey, generally you wouldn't use anything but a combination of keys for fear of interfering with other apps. So, short of modifying all of your forms, you can add this hook. With the hook comes a strong warning:

    WARNING (strong). Do not hit END do not press the VB toolbar's STOP button while the hook is running. Though keyboard hooks are less prone to crashing VB IDE in those situations, they can cause your app to stop responding to keyboard activity. Should this happen, close & save your project, close VB and restart VB.

    Ok the above being said, I added a constant you can set. Set it to True to allow hooking when in IDE else set it to False. When set to False, it will hook when app is compiled. Set it to True only to test it or test the "hot key" is functioning properly

    In the myHookProc function, change vbKeyF1 to the vbKey you want to trap -- one key only. If wanting to trap multiple keys, then a registered hotkey should probably be used.

    Ok, in a module add the code below. Required: In your startup form, the last statement in your form load, add: SetKeybdHook. In the form unload, add: ReleaseKeybdHook.
    Code:
    Option Explicit
    
    Private Const WH_KEYBOARD As Long = 2
    Private Declare Function SetWindowsHookEx Lib "user32.dll" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
    Private Declare Function CallNextHookEx Lib "user32.dll" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
    Private Declare Function UnhookWindowsHookEx Lib "user32.dll" (ByVal hHook As Long) As Long
    
    Private m_HookID As Long
    Private Const bHookInIDE As Boolean = False
    ' ^^ change to True to test functionality & when happy, change back to False
    
    Public Sub SetKeybdHook()
        If m_HookID = 0& Then
            If bHookInIDE = False Then
                On Error Resume Next
                Debug.Print 1 / 0 'Debug.Print lines of code are never compiled
                If Err Then
                    Err.Clear   ' if err, then we are in ide.
                    Exit Sub
                End If
                On Error GoTo 0
            End If
            m_HookID = SetWindowsHookEx(WH_KEYBOARD, AddressOf myHookProc, App.hInstance, App.ThreadID)
        End If
    End Sub
    Public Sub ReleaseKeybdHook()
        If Not m_HookID = 0& Then
            UnhookWindowsHookEx m_HookID
            m_HookID = 0&
        End If
    End Sub
    
    Private Function myHookProc(ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        
        'change vbKeyF1 to whatever key you want to trap
        
        If ncode = 0 Then 'HC_ACTION
            If wParam = vbKeyF1 Then    'wParam are vbKey... variables
                If lParam < 0 Then      ' key is up else key is down
                    If wParam = vbKeyF1 Then
                        Config.Show     ' what to be done when hotkey is pressed
                    End If
                End If
                myHookProc = 1      ' eat the key; don't let VB have it
            Else
                myHookProc = CallNextHookEx(m_HookID, ncode, wParam, lParam)
            End If
        Else
            myHookProc = CallNextHookEx(m_HookID, ncode, wParam, lParam)
        End If
    End Function

  5. #5
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Universal Key Press

    LaVolp,
    isn't RegisterHotKey a simple solution than #4 ?
    doesnt have to be F1

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Universal Key Press

    Quote Originally Posted by Fazi
    LaVolpe,
    isn't RegisterHotKey a simple solution than #4 ?
    Yes, but not really. Reading the MSDN documentation, can't be a single key, must be a key in combination with alt, ctrl, shift or window key. If that is desired, then it is a better solution. If a single key is needed, then no.

    There are pros & cons to both:
    1. Keyboard hook not nearly as fatal to IDE
    2. For RegisterHotKey messages...
    a. Create separate message pump. I think more of a bottleneck for messages than hook
    b. Subclass to get the WM_HotKey message. Subclassing is dangerous in IDE

  7. #7
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Universal Key Press

    Yes,yes, it seems he wanted a single key and not a combination.

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