How to Make a ShortCut HotKey in Visual Basic

Written By Nohara Binti Parahita Niya on Friday, March 29, 2013 | 5:25 PM


Maybe ShortCut HotKey function is different from the usual made in the menu. Because HotKey ShortCut ShortCut which is a global in your computer or laptop. Or it can be said Shortcut applies even if your application is not in a state of focus.

visual basic tutorial


Here's how to make it:
For Visual Basic 6.0, make a new module and type this:


Declare Function RegisterHotKey Lib "user32.dll" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As LongDeclare Function UnregisterHotKey Lib "user32.dll" (ByVal hwnd As Long, ByVal id As Long ) As Long Declare Function SetWindowLong Lib "user32.dll" Alias ​​"SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Declare Function CallWindowProc Lib "user32.dll" 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 lHotKey As Long
Public Const WM_HOTKEY = & H312 Public Const GWL_WNDPROC = -4
Public Const MOD_ALT = & H1 Public Const MOD_CTRL = & H2 Public Const MOD_SHIFT = & H4 Public Const MOD_WIN = & H8
'Id of masing2 shortcut idcommand1 Public Const Public Const idcommand2 = 101 = 102 = 103 Public Const idcommand3
CallbackMsgs Public Function (ByVal wHwnd As Long, ByVal wMsg As Long, ByVal wp_id As Long, ByVal lp_id As Long) As Long If wMsg = WM_HOTKEY Then
Form1.Show 'set focus wp_id Case idcommand1 Select Case MsgBox "command first run." Case idcommand2Msgbox "command 2 is executed."
Case idcommand3Msgbox "third command is executed."
End Select
CallbackMsgs = 1 Else CallbackMsgs = CallWindowProc (lHotKey, wHwnd, wMsg, wp_id, lp_id) End If End Function


In the 'Form_Load' type:
RegisterHotKey Me.hwnd, idcommand1, MOD_CTRL MOD_SHIFT +, Asc ("P") 'Ctrl + Shift + P RegisterHotKey Me.hwnd, idcommand2, MOD_CTRL + + MOD_SHIFT MOD_ALT, Asc ("Q")' Ctrl + Shift + Alt + Q RegisterHotKey Me.hwnd, idcommand3, MOD_WIN, Asc ("G") 'WinKey + G
lHotKey = SetWindowLong (Me.hwnd, GWL_WNDPROC, AddressOf CallbackMsgs)

In the 'Form_Unload' type:
UnregisterHotKey Me.hwnd, idcommand1 UnregisterHotKey Me.hwnd, idcommand2 UnregisterHotKey Me.hwnd, idcommand3

Then to VB.NET

In the section '(Declarations)' of type Form:

Private Declare Function RegisterHotKey Lib "user32.dll" (ByVal hwnd As Integer, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer Private Declare Function UnregisterHotKey Lib "user32.dll" (ByVal hwnd As Integer, ByVal id As Integer) As Integer = & H312 Const WM_HOTKEY
MOD_ALT = & H1 Const Const MOD_CTRL = & H2 Const MOD_SHIFT = & H4 Const MOD_WIN = & H8
'Id of masing2 shortcut Const Const idcommand2 idcommand1 = 101 = 102 = 103 Const idcommand3
Protected Overrides Sub WndProc (ByRef m As Message) MyBase.WndProc (m) If m.Msg = WM_HOTKEY Then AppActivate (System.Diagnostics.Process.GetCurrentProcess.Id) 'set focus m.WParam.ToInt32 Case idcommand1 Select Case MsgBox "command 1 run "Case idcommand2Msgbox" command 2 running "
Case idcommand3Msgbox "command 3 run"
End Select End If End Sub

In the 'Form1_Load' type:
RegisterHotKey (Me.Handle.ToInt32, idcommand1, MOD_CTRL MOD_SHIFT +, Asc ("P")) 'Ctrl + Shift + P RegisterHotKey (Me.Handle.ToInt32, idcommand2, MOD_CTRL + + MOD_SHIFT MOD_ALT, Asc ("Q")) 'Ctrl + Shift + Alt + Q RegisterHotKey (Me.Handle.ToInt32, idcommand3, MOD_WIN, Asc ("G"))' WinKey + G

In the 'Form1_FormClosed' type:
UnregisterHotKey (Me.Handle.ToInt32, idcommand1) UnregisterHotKey (Me.Handle.ToInt32, idcommand2) UnregisterHotKey (Me.Handle.ToInt32, idcommand3)