Control Hotkeys

Image Credits VNAMedia

This weekend I plan to learn coding hotkeys and sending them to Winforms, basically for fun. I was testing the Control + N hotkeys and came up with some really useful text book material. This particular code was made to test things and worked great. I was happy and posted it here to ease the work for you.

Public Const MOD_CTRL As Integer = &H2 'Control key
    Public Const WM_HOTKEY As Integer = &H312

    <DllImport("User32.dll")> _
    Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, _
                        ByVal id As Integer, ByVal fsModifiers As Integer, _
                        ByVal vk As Integer) As Integer
    End Function

    <DllImport("User32.dll")> _
    Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, _
                        ByVal id As Integer) As Integer
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, _
                        ByVal e As System.EventArgs) Handles MyBase.Load
        RegisterHotKey(Me.Handle, 100, MOD_CTRL, Keys.N)
        'RegisterHotKey(Me.Handle, 200, MOD_CTRL, Keys.C)
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_HOTKEY Then
            Dim id As IntPtr = m.WParam
            Select Case (id.ToString)
                Case "100"
                    MessageBox.Show("You pressed CTRL+N key combination")
                    '  Case "200"
                    'MessageBox.Show("You pressed ALT+C key combination")
            End Select
        End If
        MyBase.WndProc(m)
    End Sub

You should also check out these sites to know about more details.

http://www.xtremevbtalk.com/showthread.php?t=185548

http://social.msdn.microsoft.com/Forums/pl-PL/vblanguage/thread/3cc4da38-7846-4277-8dae-ef8fe4e66dc8

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/c1a24688-d844-4adc-9d85-416a7158c6ba/

http://www.eggheadcafe.com/searchform.aspx?search=WM_SYSKEYDOWN

http://www.kirsbo.com/2010/how-to-add-global-hotkeys-to-applications-in-vb-net/

Comments

Popular posts from this blog

A Good Day

My Little Wife

Fallen - Based on a true story (almost)