|
A HotKey Control
![[Pager Control]](/images/hotkey.gif)
Download the HotKey Control with source (29kb)
Your license to the code - what you can do
This sample requires the SSubTmr.DLL component. Make sure you have loaded and registered this before trying the HotKey project.
The hotkey control is one of the controls provided as part of COMCTL32.DLL, but, being of somewhat
limited utility, has never found its way into Visual Basic's controls. If you need one, however, it does
the job really well.
When it has focus, you can press CTRL, ALT and SHIFT key combinations, and it will show
the chosen key combination as a string in a text box. You can then use the key combination as, in
this example, a hot key for the application, or you could use it as a menu accelerator.
In case you want to set your application HotKey without using the control, the code to do it
is simple:
Private Const WM_SETHOTKEY = &H32
Private Declare Function SendMessageByLong Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Enum echkModifierKeys
    HOTKEYF_SHIFT = &H1
    HOTKEYF_CONTROL = &H2
    HOTKEYF_ALT = &H4
    HOTKEYF_EXT = &H8
    HOTKEYF_SHIFTCONTROL = &H3
    HOTKEYF_ALTSHIFT = &H5
    HOTKEYF_CONTROLALT = &H6
    HOTKEYF_CONTROLALTSHIFT = &H7
End Enum
Public Function SetHotKey( _
        ByVal hWnd As Long, _
        ByVal eKeyCode As VBRUN.KeyCodeConstants, _
        ByVal eModifier As echkModifierKeys _
    ) As Boolean
Dim iR As Long
Dim lKey As Long
    ' wParam is a word with the LoByte set to
    ' the key code and the HiByte set to the modifier:
    lKey = (eKeyCode And &HFF&) Or ((eModifier And &HFF&) * &H100&)
    iR = SendMessageByLong(hWnd, WM_SETHOTKEY, lKey, 0)
    Select Case iR
    Case 2
        Err.Raise 20001, App.EXEName & ".SetHotKey", "Hot key previously assigned"
    Case 1
        ' success
        SetHotKey = True
    Case 0
        Err.Raise 20002, App.EXEName & ".SetHotKey", "Invalid window for Hot key"
    Case -1
        Err.Raise 20003, App.EXEName & ".SetHotKey", "Invalid Hot key"
    Case Else
        Err.Raise 20004, App.EXEName & ".SetHotKey", "Failed to set Hot key"
    End Select
End Function
Back to top
Back to Source Code
|
  |