Public Class Form5
Const MYKEYID1 As Integer = 9
Const MYKEYID2 As Integer = 10
Public Const MOD_ALT As Integer = &H1 'Alt key
Public Const VK_NUMPAD1 As Integer = &H61 'NumPad 1 key
Public Const VK_NUMPAD2 As Integer = &H62 'NumPad 2 key
Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
Public Const WM_HOTKEY As Integer = &H312
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY And m.WParam.ToInt32 = MYKEYID1 Then
Me.Text = DateTime.Now.Second.ToString & "key 1"
ElseIf m.Msg = WM_HOTKEY And m.WParam.ToInt32 = MYKEYID2 Then
Me.Text = DateTime.Now.Second.ToString & "key 2"
End If
MyBase.WndProc(m) 'Never Forget This
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
UnregisterHotKey(Me.Handle, MYKEYID1) 'Remember to unregister the hotkey
UnregisterHotKey(Me.Handle, MYKEYID2) 'Remember to unregister the hotkey
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RegisterHotKey(Me.Handle, MYKEYID1, MOD_ALT, VK_NUMPAD1) 'Registers Alt + Numpad 1
RegisterHotKey(Me.Handle, MYKEYID2, MOD_ALT, VK_NUMPAD2) 'Registers Alt + Numpad 2
End Sub
End Class