Results 1 to 3 of 3

Thread: Urgent: How do you assign a hotkey...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 1999
    Posts
    27

    Post

    How do u assign a hotkey for a hidden form that is loaded, but like I said it is hidden, so it can't have the focus.

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    2 Methods..

    You could Fake a HotKey using the GetAsyncKeystate API within a Timer Event, eg.
    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Const VK_CONTROL = &H11
    
    Private Sub Timer1_Timer()
        If GetAsyncKeyState(VK_CONTROL) And GetAsyncKeyState(vbKeyZ) Then
            Caption = "Zoom"
        Else
            Caption = ""
        End If
    End Sub
    If you want a True HotKey, you need to use the RegisterHotKey API and SubClassing, eg.

    In a Module..
    Code:
    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
    Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
    
    Public Const MOD_ALT = &H1
    Public Const MOD_CONTROL = &H2
    Public Const MOD_SHIFT = &H4
    Public Const GWL_WNDPROC = (-4)
    Private Const WM_HOTKEY = &H312
    
    Public lPrevWnd As Long
    
    Public Function SubWindow(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        If Msg = WM_HOTKEY And wParam = 1 Then
            'Your HotKey's Been Triggered!
            Form1.Show
            MsgBox "HotKey Activated", vbInformation + vbOKOnly, "HotKey"
        End If
        SubWindow = CallWindowProc(lPrevWnd, hwnd, Msg, wParam, ByVal lParam)
    End Function
    In the Form..
    Code:
    Private Sub Form_Load()
        Call RegisterHotKey(hwnd, 1, MOD_CONTROL, vbKeyZ)
        lPrevWnd = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf SubWindow)
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        Call UnregisterHotKey(hwnd, 1)
        'Always Close the Form Properly or VB will Crash!
        Call SetWindowLong(hwnd, GWL_WNDPROC, lPrevWnd)
    End Sub
    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]


    [This message has been edited by Aaron Young (edited 11-13-1999).]

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 1999
    Posts
    27

    Post

    Thanks, that worked just like I wanted it to...

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