PostMessage in C# with ALT + C key and CTL + C key

2,786 views
Skip to first unread message

Hitendra

unread,
Sep 20, 2008, 8:31:36 AM9/20/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting, hiten...@gmail.com
Hi,

Please can any one help on how to send ALT+C and CLT+ S key press key
press event to the running application, i tried all possible what I
was able to search on net but not got the result.

Below if the code

public const UInt32 WM_CHAR = 0x0102;
public const UInt32 WM_SYSKEYDOWN = 0x0104;
public const UInt32 WM_KEYDOWN = 0x0100;

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, uint
wParam, uint lParam);

public void SendCutomMessage(IntPtr hwnd)
{
PostMessage(hwnd,WM_SYSKEYDOWN ,(unit)'C',0); // For ALT+C
wparam and lparam are correct?
PostMessage(hwnd,WM_SYSKEYDOWN ,(unit)'S',0); // For CLT+S
wparam and lparam are correct?
}

Thanks in advance.....
Hitendra Patel

Glenn

unread,
Sep 20, 2008, 10:53:51 AM9/20/08
to DotNetDe...@googlegroups.com
Are you getting the correct hwnd value for the application?  I presume that you are attempting to send it to an application other than the one that you are executing this code from.  Use FindWindow to get the hwnd for the running application.  Be aware that if the external application's window title changes, your application will not work anymore.
 
...Glenn

sallushan

unread,
Sep 20, 2008, 1:22:43 PM9/20/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I agree with Glenn, that most probably you are trying to send keys to
external application, for which you can use FindWindow().

But why are you NOT using SendKeys.Send() ?

Example:

private void Form1_Click(object sender, EventArgs e)
{
SendKeys.Send("% "); // this will send ALT+SPACE to
current application to open the Window Menu

Hitendra

unread,
Sep 21, 2008, 1:07:48 AM9/21/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Thanks Glenn for your reply,

Yes I am getting correct hwnd, and the application is running in
background say it is minimized,
and the requiremnt is there would be more instance of that application
so thats the reason I decided to
go with PostMessage but I am not getting the result.

Please can you put your thought on this?

Thanks,
Hitendra
> > Hitendra Patel- Hide quoted text -
>
> - Show quoted text -

Hitendra

unread,
Sep 21, 2008, 1:13:30 AM9/21/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Sallushan,

Thanks for your reply too, the reason for not using SendKeys is, I
have to bring each window application to front
and then only SendKeys.Send() will work, and requirement is not to do
that.

I guess you also agree on usage of PostMessage, please provide your
suggestion on PostMessage.

Thanks,
Hitendra Patel

sallushan

unread,
Sep 21, 2008, 2:25:26 PM9/21/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
OK then PostMessage is a better choice. Most probably your lParam is
NOT correct. I have grapped the Keyboard notifications for a test
application when Alt+A is pressed,

WM_SYSKEYDOWN nVirtKey:VK_MENU cRepeat:1 ScanCode:38 fExtended:0
fAltDown:1 fRepeat:0 fUp:0
wParam = 00000012
lParam = 20380001

WM_SYSKEYDOWN nVirtKey:'A' cRepeat:1 ScanCode:1E fExtended:0 fAltDown:
1 fRepeat:0 fUp:0
wParam = 00000041
lParam = 201E0001

WM_SYSKEYUP nVirtKey:'A' cRepeat:1 ScanCode:1E fExtended:0 fAltDown:1
fRepeat:1 fUp:1
wParam = 00000041
lParam = E01E0001

Note the lParam
> > - Show quoted text -- Hide quoted text -

Hitendra

unread,
Sep 22, 2008, 10:15:46 AM9/22/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Thank you very much I will give a try today,

Thanks
Hitendra
Reply all
Reply to author
Forward
0 new messages