Create a Hot Key

This code lets you create a 'hot key' for your application... the user can press a combination of keys (no matter which application they are using), and you can activate your program or run some other code, whatever program they are running. Please note that you do need your app running somewhere!

Option Explicit

'Not used in this sample, but
'these MOD constants are
'second parameter of RegisterHotKey API Function
'Public Const MOD_ALT = &H1
'Public Const MOD_CONTROL = &H2
'Public Const MOD_SHIFT = &H4

'To change default Window procedure
Public Const GWL_WNDPROC = (-4)

'When app receive this message
'then HotKey is pressed
Public Const WM_HOTKEY = &H312

'For registering and unregistering HotKey
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

'For subclassing frmHotKey
Public 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 SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

'This is for previous(default) window procedure
Public PrevWindowProc As Long
Public Function WindowProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

'If HotKey is pressed...
If Msg = WM_HOTKEY Then
'Do what you want here
    frmHotKey.WindowState = 0 'normal state
    MsgBox "HotKey was pressed.", , "HotKey message"
End If

'If End button on VB Toolbar is pressed
'then return previous(default) windows procedure and exit
If Msg = &H2 Or Msg = &H82 Or Msg = &H210 Then
SetWindowLong frmHotKey.hwnd, GWL_WNDPROC, PrevWindowProc
Exit Function
End If

'Calling previous(default) window procedure
'to proceed other messages
WindowProc = CallWindowProc(PrevWindowProc, hwnd, Msg, wParam, lParam)

End Function

For the form code, you need the following controls:

Type Name Properties
Label chkMOD Index=0,Caption=Alt+
Label chkMOD Index=1,Caption=Ctrl+
Label chkMOD Index=2,Caption=Shift+
ComboBox cmbKeys  
CommandButton cmdRegisterHotKey Caption=Register Hot Key
CommandButton cmdUnregisterHotKey Caption=Unregister Hot Key

Option Explicit

'Storage for key names
Dim strKeyNames(1 To 135) As String

Private Sub cmdRegisterHotKey_Click()

'Registering selected hot key for frmHotKey window
RegisterHotKey frmHotKey.hwnd, 1, chkMOD(0) + chkMOD(1) * 2 + chkMOD(2) * 4, cmbKeys.ListIndex + 1
'Getting previous(default) window procedure for frmHotKey window
PrevWindowProc = SetWindowLong(frmHotKey.hwnd, GWL_WNDPROC, AddressOf modHotKey.WindowProc)

MsgBox "Now this form will be minimized." & vbCrLf & "Press your HotKey to show it." & vbCrLf & vbCrLf & "THIS FORM IS SUBCLASSED - BUT YOU CAN PRESS END BUTTON ON VB TOOLBAR !!!", , "HotKey message"
frmHotKey.WindowState = 1

'Prevent from second subclassing
cmdRegisterHotKey.Enabled = False

End Sub


Private Sub cmdUnregisterHotKey_Click()

'Restore default window procedure
SetWindowLong frmHotKey.hwnd, GWL_WNDPROC, PrevWindowProc

'Unregistering HotKey with ID=1
UnregisterHotKey frmHotKey.hwnd, 1

'Enabling choose for another HotKey
cmdRegisterHotKey.Enabled = True

End Sub


Private Sub Form_Load()

'Load key names
Dim I As Integer
strKeyNames(3) = "CANCEL"
strKeyNames(8) = "BACKSPACE"
strKeyNames(9) = "TAB"
strKeyNames(12) = "CLEAR"
strKeyNames(13) = "ENTER"
strKeyNames(19) = "PAUSE"
strKeyNames(20) = "CAPS LOCK"
strKeyNames(27) = "ESC"
strKeyNames(32) = "SPACEBAR"
strKeyNames(33) = "PAGE UP"
strKeyNames(34) = "PAGE DOWN"
strKeyNames(35) = "END"
strKeyNames(36) = "HOME"
strKeyNames(37) = "LEFT ARROW"
strKeyNames(38) = "UP ARROW"
strKeyNames(39) = "RIGHT ARROW"
strKeyNames(40) = "DOWN ARROW"
strKeyNames(41) = "SELECT"
strKeyNames(43) = "EXECUTE"
strKeyNames(44) = "PRINT SCREEN"
strKeyNames(45) = "INS"
strKeyNames(46) = "DEL"
strKeyNames(47) = "HELP"

' 0-9
For I = 48 To 57
strKeyNames(I) = Chr$(I)
Next I

' A-Z
For I = 65 To 90
strKeyNames(I) = Chr$(I)
Next I

strKeyNames(91) = "Left Windows key"
strKeyNames(92) = "Right Windows key"
strKeyNames(93) = "Applications key"

'Numeric keypad 0-9
For I = 96 To 105
strKeyNames(I) = "Numeric keypad " & Chr$(I - 48)
Next I

strKeyNames(106) = "Multiply"
strKeyNames(107) = "Add"
strKeyNames(108) = "Separator"
strKeyNames(109) = "Substract"
strKeyNames(110) = "Decimal"
strKeyNames(111) = "Divide"

' F1-F24
For I = 112 To 135
strKeyNames(I) = "F" & Str$(I - 111)
Next I

'Fill combo box
For I = 1 To 135
cmbKeys.AddItem strKeyNames(I)
Next I

'Setting default hotkey (DOWN ARROW :)
cmbKeys.ListIndex = 39

End Sub


Private Sub Form_Unload(Cancel As Integer)

'Unregistering HotKey with ID=1 before exit
UnregisterHotKey frmHotKey.hwnd, 1

End Sub

You might also like...

Comments

Georgi Ganchev

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The generation of random numbers is too important to be left to chance.” - Robert R. Coveyou