Shortcut HotKey
Berbeda dengan shortcut yang bisa dibuat pada menu, shortcut hotkey merupakan shortcut yang bersifat global di komputer. Dengan kata lain shortcut ini berlaku meskipun aplikasi Anda tidak sedang dalam keadaan focus. Berikut ini contoh cara membuatnya.
[ VB 6.0 ]
Buat Project baru dengan sebuah Form dan sebuah Module.
Di Module ketikkan :
Declare Function RegisterHotKey Lib "user32.dll" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Declare 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 dari masing2 shortcut
Public Const idPerintah1 = 101
Public Const idPerintah2 = 102
Public Const idPerintah3 = 103
Public Function CallbackMsgs(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
Select Case wp_id
Case idPerintah1
Form1.Caption = "Perintah Pertama"
Case idPerintah2
Form1.Caption = "Perintah Kedua"
Case idPerintah3
Form1.Caption = "Perintah Ketiga"
End Select
CallbackMsgs = 1
Else
CallbackMsgs = CallWindowProc(lHotKey, wHwnd, wMsg, wp_id, lp_id)
End If
End Function
Declare 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 dari masing2 shortcut
Public Const idPerintah1 = 101
Public Const idPerintah2 = 102
Public Const idPerintah3 = 103
Public Function CallbackMsgs(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
Select Case wp_id
Case idPerintah1
Form1.Caption = "Perintah Pertama"
Case idPerintah2
Form1.Caption = "Perintah Kedua"
Case idPerintah3
Form1.Caption = "Perintah Ketiga"
End Select
CallbackMsgs = 1
Else
CallbackMsgs = CallWindowProc(lHotKey, wHwnd, wMsg, wp_id, lp_id)
End If
End Function
Di bagian 'Form_Load' ketikkan :
RegisterHotKey Me.hwnd, idPerintah1, MOD_CTRL + MOD_SHIFT, Asc("P") 'Ctrl + Shift + P
RegisterHotKey Me.hwnd, idPerintah2, MOD_CTRL + MOD_SHIFT + MOD_ALT, Asc("Q") 'Ctrl + Shift + Alt + Q
RegisterHotKey Me.hwnd, idPerintah3, MOD_WIN, Asc("G") 'WinKey + G
lHotKey = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf CallbackMsgs)
RegisterHotKey Me.hwnd, idPerintah2, MOD_CTRL + MOD_SHIFT + MOD_ALT, Asc("Q") 'Ctrl + Shift + Alt + Q
RegisterHotKey Me.hwnd, idPerintah3, MOD_WIN, Asc("G") 'WinKey + G
lHotKey = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf CallbackMsgs)
Di bagian 'Form_Unload' ketikkan :
UnregisterHotKey Me.hwnd, idPerintah1
UnregisterHotKey Me.hwnd, idPerintah2
UnregisterHotKey Me.hwnd, idPerintah3
UnregisterHotKey Me.hwnd, idPerintah2
UnregisterHotKey Me.hwnd, idPerintah3
[ VB . NET ]
Buat Project baru dengan sebuah Form.
Di bagian '(Declarations)' dari Form ketikkan :
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
Const WM_HOTKEY = &H312
Const MOD_ALT = &H1
Const MOD_CTRL = &H2
Const MOD_SHIFT = &H4
Const MOD_WIN = &H8
'id dari masing2 shortcut
Const idPerintah1 = 101
Const idPerintah2 = 102
Const idPerintah3 = 103
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
Select Case m.WParam.ToInt32
Case idPerintah1
Me.Text = "Perintah Pertama"
Case idPerintah2
Me.Text = "Perintah Kedua"
Case idPerintah3
Me.Text = "Perintah Ketiga"
End Select
End If
End Sub
Private Declare Function UnregisterHotKey Lib "user32.dll" (ByVal hwnd As Integer, ByVal id As Integer) As Integer
Const WM_HOTKEY = &H312
Const MOD_ALT = &H1
Const MOD_CTRL = &H2
Const MOD_SHIFT = &H4
Const MOD_WIN = &H8
'id dari masing2 shortcut
Const idPerintah1 = 101
Const idPerintah2 = 102
Const idPerintah3 = 103
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
Select Case m.WParam.ToInt32
Case idPerintah1
Me.Text = "Perintah Pertama"
Case idPerintah2
Me.Text = "Perintah Kedua"
Case idPerintah3
Me.Text = "Perintah Ketiga"
End Select
End If
End Sub
Di bagian 'Form1_Load' ketikkan :
RegisterHotKey(Me.Handle.ToInt32, idPerintah1, MOD_CTRL + MOD_SHIFT, Asc("P")) 'Ctrl + Shift + P
RegisterHotKey(Me.Handle.ToInt32, idPerintah2, MOD_CTRL + MOD_SHIFT + MOD_ALT, Asc("Q")) 'Ctrl + Shift + Alt + Q
RegisterHotKey(Me.Handle.ToInt32, idPerintah3, MOD_WIN, Asc("G")) 'WinKey + G
RegisterHotKey(Me.Handle.ToInt32, idPerintah2, MOD_CTRL + MOD_SHIFT + MOD_ALT, Asc("Q")) 'Ctrl + Shift + Alt + Q
RegisterHotKey(Me.Handle.ToInt32, idPerintah3, MOD_WIN, Asc("G")) 'WinKey + G
Di bagian 'Form1_FormClosed' ketikkan :
UnregisterHotKey(Me.Handle.ToInt32, idPerintah1)
UnregisterHotKey(Me.Handle.ToInt32, idPerintah2)
UnregisterHotKey(Me.Handle.ToInt32, idPerintah3)
UnregisterHotKey(Me.Handle.ToInt32, idPerintah2)
UnregisterHotKey(Me.Handle.ToInt32, idPerintah3)
CATATAN : Jika shortcut tidak berjalan, maka terjadi error yang bisa dikarenakan karena shortcut tersebut telah dipakai Windows atau ada aplikasi lain yang terlebih dulu memakainya.
Label: (Menengah), VB .NET, VB 6.0
0 Komentar:
Posting Komentar
Pengunjung yang baik selalu meninggalkan jejak berupa komentar. :)
Berlangganan Posting Komentar [Atom]
<< Beranda