Question

I coded this and it was working earlier! a global hotkey for alt+1 and alt+2. As i was closing my project i accidentally pressed a key or something and now for some reason only alt+1 is working, and no other combination of alt+ a number...

    [DllImport("user32.dll")]
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
    [DllImport("user32.dll")]
    public static extern bool UnregisterHotKey(IntPtr hWnd, int id);


    [DllImport("User32.dll")]
    private static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
    [DllImport("User32.dll")]
    private static extern short GetAsyncKeyState(System.Int32 vKey);
    const int MYACTION_HOTKEY_ID = 1;

    public Form1()
    {
        InitializeComponent();
        RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 1, (int)Keys.D1);
        RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 1, (int)Keys.D2);
    }

    protected override void WndProc(ref Message m)
    {

        if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID && (GetAsyncKeyState(Keys.D1) == -32767))
        {
            Console.Write("alt+1");
        }
        if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID && (GetAsyncKeyState(Keys.D2) == -32767))
        {
            Console.Write("alt+2");
        }
        base.WndProc(ref m);
    }

Anyone have any ideas?

Was it helpful?

Solution

The second parameter to RegisterHotkey() is for you to specify the ID of the combination. Each combination should have its own unique ID (unique to the window handle associated with the key combo at least). You can tell which hotkey was pressed as the ID is passed to m.WParam in WndProc(). Here's an example with alt-1 having an ID of 1001, and alt-2 having an ID of 1002:

public partial class Form1 : Form
{

    private const int WM_HOTKEY = 0x0312;
    private const int WM_DESTROY = 0x0002;

    [DllImport("user32.dll")]
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
    [DllImport("user32.dll")]
    public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    public Form1()
    {
        InitializeComponent();
        RegisterHotKey(this.Handle, 1001, 1, (int)Keys.D1);
        RegisterHotKey(this.Handle, 1002, 1, (int)Keys.D2);
    }

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_HOTKEY:
                switch (m.WParam.ToInt32())
                {
                    case 1001:
                        Console.Write("alt+1");
                        break;

                    case 1002:
                        Console.Write("alt+2");
                        break;

                }
                break;

            case WM_DESTROY:
                UnregisterHotKey(this.Handle, 1001);
                UnregisterHotKey(this.Handle, 1002);
                break;
        }
        base.WndProc(ref m);
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top