Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
 
 
 
500MB 27GB Web Hosting - $9.95/Month
 
 
 
 
 
Old Pages
 
Old Index
Site Map
What's New
 
Books
How To
Tips & Tricks
Tutorials
Stories
Performance
Essays
Links
Q & A
New in VB6
Free Stuff
Pictures
 
 
 
TitleInstall and deinstall hotkeys
Keywordshotkey, accelerator, install hotkey, RegisterHotKey
CategoriesUtilities, Windows, API, Tips and Tricks
 
A hotkey is a key combination that you can use to trigger an action anywhere in Windows.

When the form loads, use the RegisterHotKey API function to install the hotkey (in this example, Alt-F10). Then subclass to watch for the WM_HOTKEY message.

 
Private Sub Form_Load()
    ' Register the hotkey.
    If RegisterHotKey(hWnd, HOTKEY_ID, _
        MOD_ALT, VK_F10) = 0 _
    Then
        MsgBox "Error registering hotkey."
        Unload Me
    End If

    ' Subclass the TextBox to watch for
    ' WM_HOTKEY messages.
    OldWindowProc = SetWindowLong( _
        hWnd, GWL_WNDPROC, _
        AddressOf NewWindowProc)
End Sub
 
When the new WindowProc sees the WM_HOTKEY message, it calls the form's public Hotkey subroutine.
 
' Look for the WM_HOTKEY message.
Public Function NewWindowProc(ByVal hWnd As Long, ByVal Msg _
    As Long, ByVal wParam As Long, ByVal lParam As Long) As _
    Long
Const WM_NCDESTROY = &H82
Const WM_HOTKEY = &H312

    ' If we're being destroyed,
    ' restore the original WindowProc and
    ' unregister the hotkey.
    If Msg = WM_NCDESTROY Then
        SetWindowLong _
            hWnd, GWL_WNDPROC, _
            OldWindowProc

        UnregisterHotKey hWnd, HOTKEY_ID
    End If

    ' See if this is the WM_HOTKEY message.
    If Msg = WM_HOTKEY Then Form1.Hotkey

    ' Process the message normally.
    NewWindowProc = CallWindowProc( _
        OldWindowProc, hWnd, Msg, wParam, _
        lParam)
End Function
 
In this example, the Hotkey subroutine adds the time to the form's TextBox. It then makes sure that the form is not minimized and gives the form the focus.
 
' We got the hotkey.
Public Sub Hotkey()
    txtTimes.Text = txtTimes.Text & _
        Time & vbCrLf
    txtTimes.SelStart = Len(txtTimes.Text)

    If Me.WindowState = vbMinimized Then Me.WindowState = _
        vbNormal
    Me.SetFocus
End Sub
 
 
Copyright © 1997-2003 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated