1

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);
    }
user779444
  • 1,365
  • 4
  • 21
  • 38

2 Answers2

0

In my understanding, it should be enough to send WM_SYSKEYDOWN / WM_KEYDOWN. see Docs

DanielB
  • 19,910
  • 2
  • 44
  • 50
  • You have to send a WM_KEYUP for VK_SNAPSHOT I think. Are you trying to make multiple snapshots. Btw. if you want a screenshot of a window have a look at [Managed Windows API](http://mwinapi.sourceforge.net/) and `ManagedWinapi.Windows.SystemWindow.Image` – DanielB Jun 07 '11 at 16:17
  • Keycodes are [here](http://msdn.microsoft.com/en-us/library/dd375731(VS.85).aspx) `VK_MENU 0x12 ALT key` `VK_SNAPSHOT 0x2C PRINT SCREEN key` – DanielB Jun 07 '11 at 16:26
0

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.

Guvante
  • 18,775
  • 1
  • 33
  • 64
  • Tried 29, same as 1, it just does that anoyying windows sound, like when you try to type in a message box – user779444 Jun 07 '11 at 16:17
  • Forget it, it's something with the key code. I don't know what are the correct ones. I try to put 0x70, it sends F1. I try to put 0x46 and it does that sound. – user779444 Jun 07 '11 at 16:20
  • @user779444: Bit 29 would be 0x10000000 actually. Most low numbers will simply tell the target program that you hit the button repeatedly (up to 0xFFFF) – Guvante Jun 07 '11 at 16:35