Results 1 to 7 of 7

Thread: Hotkey horror

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2000
    Location
    Sydney, Nova Scotia
    Posts
    55
    I want to be able to assign 4 different tasks to the Hotkeys F9, F10, F11, F12 on my application when it doesn't have the focus.

    Would someone please show me an example of this with MsgBoxs or something......

    I've been trying different methods all day and none have been successful.

    thanks
    Vince McMullin
    Aka Vam, Vinny Mac

    Its just that simple

  2. #2

  3. #3
    Guest
    Set the KeyPreview property = True

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyF9
    Call SomeProcedure()
    Case vbKeyF10
    Call SomeOtherProcedure()
    Case vbKeyF11
    Call SomethingElse()
    Case vbKeyF12
    Call StillSomethingElse()
    End Select
    End Sub


    hope this helps..

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    In a Module:
    Code:
    Private Type POINTAPI
            x As Long
            y As Long
    End Type
    
    Private Type MSG
        hwnd As Long
        message As Long
        wParam As Long
        lParam As Long
        time As Long
        pt As POINTAPI
    End Type
    
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
    Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
    Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
    Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Const WH_GETMESSAGE = 3
    Private Const WM_HOTKEY = &H312
    
    Public Const MOD_ALT = &H1
    Public Const MOD_CONTROL = &H2
    Public Const MOD_SHIFT = &H4
        
    Private lHookID As Long
    Private lHotKeys As Long
    
    Private Function CallBackHook(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Dim tMSG As MSG
            
        CopyMemory tMSG, ByVal lParam, Len(tMSG)
        If tMSG.message = WM_HOTKEY And wParam Then
            'Execute whatever for the HotKey here
            'Each Hotkey is identified by it's unique ID
            'The first hotkey, wParam = 1, 2nd, wParam = 2, etc..
            Form1.lPressed = tMSG.wParam
            Form1.Command1_Click
        End If
        If nCode < 0 Then CallBackHook = CallNextHookEx(lHookID, nCode, wParam, ByVal lParam)
    End Function
    
    Public Function SetHotKey(ByVal lSpecial As Long, ByVal lKey As Long) As Long
        Static lHotKeyID As Long
        
        lHotKeyID = lHotKeyID + 1
        If RegisterHotKey(0&, lHotKeyID, lSpecial, lKey) <> 0 Then
            lHotKeys = lHotKeys + 1
            SetHotKey = lHotKeyID
            If lHookID = 0 Then lHookID = SetWindowsHookEx(WH_GETMESSAGE, AddressOf CallBackHook, App.hInstance, App.ThreadID)
        End If
        
    End Function
    
    Public Sub RemoveHotKey(ByVal lHotKeyID As Long)
        If UnregisterHotKey(0&, lHotKeyID) Then
            If lHotKeys > 0 Then lHotKeys = lHotKeys - 1
        End If
        If lHotKeys = 0 And lHookID <> 0 Then
            Call UnhookWindowsHookEx(lHookID)
            lHookID = 0
        End If
    End Sub
    Example usage:
    Code:
    Private lHotKey(3) As Long
    Public lPressed As Long
    
    Private Sub Form_Load()
        lHotKey(0) = SetHotKey(0, vbKeyF9)
        lHotKey(1) = SetHotKey(0, vbKeyF10)
        lHotKey(2) = SetHotKey(0, vbKeyF11)
        lHotKey(3) = SetHotKey(0, vbKeyF12)
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        Dim lIndex As Long
        For lIndex = 0 To 3
            RemoveHotKey lHotKey(lIndex)
        Next
    End Sub
    
    Public Sub Command1_Click()
        'Make the Command Buttons Click Event Public, so it can be called outside the Forms Module.
        MsgBox "You pressed the Hotkey with the ID of: " & lPressed, vbSystemModal
    End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2000
    Location
    Sydney, Nova Scotia
    Posts
    55
    Thanks Aaron for the code it works wonderfully its what I'm looking for except, I don't understand the use of the command button?

    And with that I'm still haveing troubles assigning a task to a certain key....
    Such as
    Hotkey F9 would send a message box saying "1"
    Hotkey F10 would send a message box saying "2"
    Etc.........

    If you could show me where to plug this in successfully without creating errors I would be thank ful......
    Vince McMullin
    Aka Vam, Vinny Mac

    Its just that simple

  6. #6
    New Member
    Join Date
    Sep 2000
    Posts
    1

    Lightbulb

    'this should work:

    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

    Private Sub Timer1_Timer()
    If GetAsyncKeyState(120) Then
    MsgBox "f9 was pressed ;]"
    End If
    End Sub

    '120 is f9, 121 is f10, 122 is f11...

  7. #7
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    Using Aaron's code, in the command click event, have this:
    Code:
    Public Sub Command1_Click()
        'Make the Command Buttons Click Event Public, so it can be called outside the Forms Module.
    Select Case lPressed
    Case 1
    ' do F9 stuff
    Case 2
    ' do F10 Stuff
    End Select
    End Sub
    The Command1_Click is just a sub getting called, you can change that in the module code if you want.

    Any sub will do
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

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