3

I trying to send in my application RIGHT ALT + C.

I tried do it following:

PostMessage(hWindow, WM_KEYDOWN, (IntPtr)0x0043, (IntPtr)0x0012);
Delay(1000);
PostMessage(hWindow, WM_KEYUP, (IntPtr)0x0043, (IntPtr)0x0012);

and

PostMessage(hWindow, WM_KEYDOWN, (IntPtr)0x0043, (IntPtr)0x0001);
Delay(1000);
PostMessage(hWindow, WM_KEYUP, (IntPtr)0x0043, (IntPtr)0x0001);

but it doesn't correctly. How should I used it?

Dharman
  • 30,962
  • 25
  • 85
  • 135
cadi2108
  • 1,280
  • 6
  • 19
  • 43
  • 2
    You cannot fake the keyboard state with PostMessage, your program won't see the ALT key held down. SendInput is required which breaks the option to make it specific to only one window. There is typically some other message generated in response to the ALT+C keystroke, typically WM_COMMAND. Fake that one. – Hans Passant Jul 08 '12 at 17:05
  • But I need to send it to program in background... – cadi2108 Jul 08 '12 at 18:08
  • Yes, sure, that's what every says when they ask this question. But programs were invariably written and tested with the expectation that they are in the foreground when they process keyboard input. And don't work well when they are not. There's a programmer somewhere that can help you fix the program, we can't help you find him. – Hans Passant Jul 08 '12 at 18:58

3 Answers3

3

Yes, it's possible using PostMessage. I used program Spy++ (it's inside Visual Studio or you can download it http://mdb-blog.blogspot.com/2010/11/microsoft-spy-or-spyxx-for-download.html) I runed notepad.exe and Spy++, and used Function FindWindow to handle this notepad. Then I used Spy->LogMessages, in Tab Messages I cleared all and stay checkbox Keyboard and in output I checked Raw Message Parameters.

Then in notepad i pressed alt+1 and I noted values from Window Messages. I known now, where use WM_KEYDOWN, WM_KEYUP, WM_SYSKEYUP etc. and value lParam, wParam.

My function:

    public void PressKeyWithAlt(uint key, uint lParamDown, uint lParamUp)
    {
        PostMessage(hWindow, WM_KEYDOWN, VK_CONTROL, 0x001D0001);
        PostMessage(hWindow, WM_KEYDOWN, VK_MENU, 0x21380001);
        PostMessage(hWindow, WM_KEYDOWN, key, lParamDown);
        Delay(1000);
        PostMessage(hWindow, WM_KEYUP, key, lParamUp);
        PostMessage(hWindow, WM_SYSKEYUP, VK_CONTROL, 0xE01D0001);
        PostMessage(hWindow, WM_KEYUP, VK_MENU, 0xC1380001);
    }

and for example for alt+1

PressKeyWithAlt(VK_1, 0x20020001, 0xE0020001);

Thank you for all help.

cadi2108
  • 1,280
  • 6
  • 19
  • 43
  • 1
    Hello i did exactly what you did but it doesn't work. here my question in stackoverflow http://stackoverflow.com/questions/25659399/altkey-not-working-with-postmessageuse32-dll if you can help me i will be very grateful. – Vladimir Potapov Sep 04 '14 at 07:17
2

Try sending WM_SYSKEYDOWN and WM_SYSKEYUP messages instead. They are generated when either F10 or ALT+somekey is pressed and released.

Monroe Thomas
  • 4,962
  • 1
  • 17
  • 21
0

Interleave them, like

alt down
c down
delay
c up
alt up
Daniel
  • 30,896
  • 18
  • 85
  • 139