Click here to Skip to main content
15,794,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I want to send keystrokes to window that is not focused. To another application.
Can you show me some tutorials or, tell me how.

Sergey Alexandrovich Kryukov 21-Nov-11 3:26am    
Why doing so?

I found this, but I don't know how where to found the key codes for "WM_KEYDOWN", do you know a list of all the keys transformed for "WM_KEYDOWN"?

[DllImport("User32.Dll", EntryPoint = "PostMessageA", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

PostMessage(winHandle, (uint)WM_KEYDOWN, (IntPtr)66, IntPtr.Zero);
Share this answer
Basically, this is a bad idea. It depends on the application to be "controlled" this way. You cannot just use SendInput, so you need to rely on some message processing using HWND and P/Invoked SendMessage/PostMessage without reliable results.

If this is your application and you want to use this method just for UI development, this is all wrong: you can always use "legal" programming method. If not, any results are not reliable. There is a good principle: develop software, not integrate. And this is certainly not a good method of "integration".

Perhaps, explain you ultimate goals to get a chance to get some positive advice.

Share this answer
Mitran Stefanita Alexandru 21-Nov-11 3:38am    
I have to do that same thing every day, and I thought that I can make a small C# program to do that thing for me and save me of some time, I'm not doing this to sell the application or something like that, I just want to help my work. Can you make me a small code to send a message to notepad, when the notepad is not a foreground window. Pleas, it could really help me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900