HotKey Control (VBCCRHotKey)
The VBCCRHotKey control allows users to set and capture keyboard shortcut combinations. This control is typically used in custom shortcut settings interfaces, allowing users to customize application shortcuts.
Properties
Key Properties
HotKey
: Currently set shortcut combinationInvalidKeys
: Set disabled key combinationsInvalidCombinations
: Set disabled modifier combinationsModifiers
: Current modifier key state (Alt, Ctrl, Shift)KeyCode
: Current key codeAutoRepeat
: Whether to allow key repetitionEnabled
: Enable/disable controlBackColor
: Background colorForeColor
: Text color
Methods
Main Methods
SetHotKey(KeyCode As Long, Modifiers As Long)
: Set shortcutGetHotKey()
: Get current shortcutSetFocus()
: Set focus to controlRefresh()
: Refresh display
Events
Change()
: Triggered when shortcut changesInvalidKey()
: Triggered when invalid key is inputGotFocus()
: Triggered when control receives focusLostFocus()
: Triggered when control loses focus
Code Examples
Basic Usage
vb
Private Sub Form_Load()
With HotKey1
' Set initial shortcut (Ctrl+S)
.SetHotKey vbKeyS, HOTKEYF_CONTROL
' Disable certain combinations
.InvalidCombinations = HOTKEYF_ALT Or HOTKEYF_CONTROL Or HOTKEYF_SHIFT
End With
End Sub
Monitoring Shortcut Changes
vb
Private Sub HotKey1_Change()
Dim KeyCode As Long
Dim Modifiers As Long
' Get current shortcut
KeyCode = HotKey1.KeyCode
Modifiers = HotKey1.Modifiers
' Display current combination
ShowHotKeyCombo KeyCode, Modifiers
End Sub
Private Sub ShowHotKeyCombo(ByVal KeyCode As Long, ByVal Modifiers As Long)
Dim strHotKey As String
If (Modifiers And HOTKEYF_CONTROL) Then strHotKey = strHotKey & "Ctrl+"
If (Modifiers And HOTKEYF_ALT) Then strHotKey = strHotKey & "Alt+"
If (Modifiers And HOTKEYF_SHIFT) Then strHotKey = strHotKey & "Shift+"
strHotKey = strHotKey & Chr$(KeyCode)
lblHotKey.Caption = "Current Shortcut: " & strHotKey
End Sub
Shortcut Validation
vb
Private Sub ValidateHotKey()
Dim KeyCode As Long
Dim Modifiers As Long
' Get current combination
KeyCode = HotKey1.KeyCode
Modifiers = HotKey1.Modifiers
' Validate if combination is valid
If IsValidHotKey(KeyCode, Modifiers) Then
SaveHotKey KeyCode, Modifiers
Else
MsgBox "Invalid shortcut combination"
End If
End Sub
Private Function IsValidHotKey(ByVal KeyCode As Long, ByVal Modifiers As Long) As Boolean
' Check if at least one modifier is included
If Modifiers = 0 Then