Skip to content
Jacob Fliss edited this page Dec 1, 2020 · 7 revisions

RESOURCES

Hot Key sample project

Virtual key list

RegisterHotKey Function

Control.WndProc Method

C# CODE

Add Interop Services above the namespace:

using System.Runtime.InteropServices;

Add the following to the form class:

#region DllImports
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, uint vlc);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
#endregion

protected override void WndProc(ref Message msg)
{
    if (msg.Msg == 0x0312) //our hot button recognition and function triggering
    {
        int id = msg.WParam.ToInt32();
        if (id == 1) { //our designated ID to execute
            execFunction(); //our function to execute
        }
        if (id == 2) { //our designated ID to execute
            execSecondFunction(); //our second function to execute
        }
    }
    base.WndProc(ref msg);
 }

In your form load or initialize function register the hotkey like so:

public Form1()
{
    RegisterHotKey(this.Handle, 1, 0x0002, '1'); //hold down CTRL (0x0002) and press 1 key, trigger ID 1 in WndProc
    RegisterHotKey(this.Handle, 2, 0x0000, '2'); //second example. Just press 2 key, trigger ID 2 in WndProc
}

Our execFunction(). This is just an example function, change it to whatever you want:

void execFunction() {
    MessageBox.Show("You pressed the hotkey!");
}

void execSecondFunction() {
    MessageBox.Show("You pressed the 2nd hotkey!");
}

VB.Net

Imports System.Runtime.InteropServices

Public Class Form1
    <DllImport("user32.dll", EntryPoint:="RegisterHotKey")>
    Private Shared Function RegisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    <DllImport("user32.dll", EntryPoint:="UnregisterHotKey")>
    Private Shared Function UnregisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    Protected Overrides Sub WndProc(ByRef msg As Message)
        If msg.Msg = &H312 Then
            Dim id As IntPtr = msg.WParam
            Select Case (id.ToString) 'our designated ID to execute
                Case "1"
                    execFunction() 'our function to execute
                Case "2"
                    execSecondFunction() 'our second function to execute
            End Select
        End If
        MyBase.WndProc(msg)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RegisterHotKey(Me.Handle, 1, &H2, Keys.D1) 'hold down CTRL (0x0002) and press 1 key, trigger ID 1 in WndProc
        RegisterHotKey(Me.Handle, 2, 0, Keys.D2) 'second example. Just press 2 key, trigger ID 2 in WndProc
    End Sub

    Private Sub execFunction()
        MessageBox.Show("You pressed the hotkey!")
    End Sub

    Private Sub execSecondFunction()
        MessageBox.Show("You pressed the 2nd hotkey!")
    End Sub

End Class