Search code examples
c#winformskeywindows-messageskeyhook

Holding Alt Button Using Windows Messages


How can I, with windows messages, Hold the ALT button?

Is there a WM_KEYHOLD or anything like that?

This is the code to screen shot but I guess something is missin. I think the key codes im using are bad, For 0x70 it sends F1. and for 0x46 it sends anoyying windows sound.

    const uint WM_SYSKEYDOWN = 260;
    const uint VK_MENU = 18;//virtual key code of Alt key
    const uint VK_SNAPSHOT = 44;//virtual key code of Snapshot key

    [DllImport("User32.Dll")]
    public static extern long PostMessage(IntPtr hWnd, UInt32 wMsg, long wParam, long lParam);

    [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(String sClassName, String sAppName);

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr GetForegroundWindow();

    private void timer1_Tick(object sender, EventArgs e)
    {
        IntPtr hwnd = GetForegroundWindow();
        //PostMessage(hwnd, WM_SYSKEYDOWN, VK_MENU, 1);
        PostMessage(hwnd, WM_SYSKEYDOWN, VK_SNAPSHOT, 1);
    }

Solution

  • The Docs linked by DanielB reference to lParam's bit 29 defining ALT status, have you tried that?

    Bit 29 - The context code. The value is 1 if the ALT key is down while the key is pressed; it is 0 if the WM_SYSKEYDOWN message is posted to the active window because no window has the keyboard focus.