By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

C# API Sendmessage or PostMessage to replace SendKeys - Help Needed

P: 18
Hello everyone.

I have what I believe to be a simple question, yet I can not find anything that is helpful to me.

Basically I have an application, that I want to use to control external applications. My program will run either embedded within another program or minimized to the system tray. So SendKeys is out because it only works if the controlled application has focus, which it will not.

So my conclusion is the windows API. I have never used the windows API, and from my experience the past few days I am not sure I want to!

So I am using user32.dll like this (these are not written in stone and are used by looking at other examples):
Expand|Select|Wrap|Line Numbers
  1. [DllImport("user32.dll")]
  2.         public static extern int FindWindow(
  3.         string lpClassName, // class name
  4.         string lpWindowName // window name
  5.         );
  6. [DllImport("user32.dll")]
  7.         public static extern int SendMessage(
  8.         int hWnd, // handle to destination window
  9.         uint Msg, // message
  10.         int wParam, // first message parameter
  11.         int lParam // second message parameter
  12.         );
  13.  
FindWindow() works perfectly. I can get the handle of the window. And the sendmessage function works as well with the example I pulled it from which calls the close command of the window like this:

Expand|Select|Wrap|Line Numbers
  1. const int WM_SYSCOMMAND = 0x018;
  2. const int SC_CLOSE = 0x053;
  3.  
  4. int WindowToFind = FindWindow(null,"Untitled - Notepad");
  5. int result = SendMessage(WindowToFind, WM_SYSCOMMAND, SC_CLOSE, 0);
  6.  
Now this is all fine and dandy, but the question becomes, how do I send the keypress "A" or "B" or "Ctrl+D" to some window?

I read that the command "WM_KEYDOWN = 0x0100" should be used, but what is the argument? Is it the character code in hex? Is it some random thing? I have not seen any working code or full samples of code anywhere. If someone could link me to some fully working code to do this, or just post a sample I would be highly appreciative.

Thankyou in advance...
Apr 22 '07 #1
Share this Question
Share on Google+
11 Replies


P: 18
Anyone? Bueler, bueler?
Apr 23 '07 #2

P: 18
Doe nobody know how to send keystrokes through the windows API?
Apr 23 '07 #3

RedSon
Expert 5K+
P: 5,000
Anyone? Bueler, bueler?
Instead of trying to be "cute" by making a movie reference that probably only 10% of the people here have seen, why don't you try posting to the correct forum. The .NET forum is where the C# questions are answered not the c/c++ forum. But lucky for you I moderate both and can move this thread over. Next time pay better attention to what you are doing.
Apr 23 '07 #4

RedSon
Expert 5K+
P: 5,000

Expand|Select|Wrap|Line Numbers
  1. const int WM_SYSCOMMAND = 0x018;
  2. const int SC_CLOSE = 0x053;
  3.  
  4. int WindowToFind = FindWindow(null,"Untitled - Notepad");
  5. int result = SendMessage(WindowToFind, WM_SYSCOMMAND, SC_CLOSE, 0);
  6.  
Now this is all fine and dandy, but the question becomes, how do I send the keypress "A" or "B" or "Ctrl+D" to some window?

I read that the command "WM_KEYDOWN = 0x0100" should be used, but what is the argument? Is it the character code in hex? Is it some random thing? I have not seen any working code or full samples of code anywhere. If someone could link me to some fully working code to do this, or just post a sample I would be highly appreciative.

Thankyou in advance...
You can use SendMessage the same way as before but instead of WM_SYSCOMMAND you make it WM_KEYDOWN and instead of SC_CLOSE you need to pass the hex value of the keypress or see if the VK_ key mappings will work for you. Also be aware how WM_KEYDOWN messages are handled you may need to make up special WPARAM and LPARAM parameters in order to process Shift+ Ctrl+ and Alt+ keys.
Apr 23 '07 #5

RedSon
Expert 5K+
P: 5,000
Here you go:

The structure of a WM_KEYDOWN message: http://msdn2.microsoft.com/en-us/library/ms646280.aspx
Apr 23 '07 #6

RedSon
Expert 5K+
P: 5,000
You might also want to consider sending a WM_KEYUP message since it is almost impossible to only press a key down and never up on a computer system. Some applications my not respond to input until they receive both a KEYDOWN and KEYUP message.
Apr 23 '07 #7

P: 18
Instead of trying to be "cute" by making a movie reference that probably only 10% of the people here have seen, why don't you try posting to the correct forum. The .NET forum is where the C# questions are answered not the c/c++ forum. But lucky for you I moderate both and can move this thread over. Next time pay better attention to what you are doing.
I honestly didnt even see the .NET area. sorry. I figured C# fits into C++/C better than basic or java. sorry again.
Apr 24 '07 #8

P: 18
You can use SendMessage the same way as before but instead of WM_SYSCOMMAND you make it WM_KEYDOWN and instead of SC_CLOSE you need to pass the hex value of the keypress or see if the VK_ key mappings will work for you. Also be aware how WM_KEYDOWN messages are handled you may need to make up special WPARAM and LPARAM parameters in order to process Shift+ Ctrl+ and Alt+ keys.
I thought that I had tried that, but ti didn't work...

WM_KEYDOWN = 0x0100

or so I believe.

Then for instance to send the 'A' key, it would be: 0x0041 isnt it?

So then SendMessage(window, 0x0100, 0x0041, 0) should do it right? Well I cant seem to get it to work with Notepad for example.

Could you post a full example please?
Apr 24 '07 #9

P: 18
Here you go:

The structure of a WM_KEYDOWN message: http://msdn2.microsoft.com/en-us/library/ms646280.aspx
Yes I have seen that, but honestly makes no sense whatsoever to me.

Is that the bit makeup of the 32bit integer?

so if I want to send it once, then bits 0-15 are <000000000000001>
and for the letter A it is for bits 16 through 23 <01000001>
and then not extended key so bit 24 is <0>
and 25-31 are all zero because some are reserved and the others must be down for a WM_KEYDOWN message.

So total would be <00000000010000010000000000000001> for the byte to send. That in decimal form is: 4259841 and hex would be 0x410001

so the total message would be SendMessage(Window, 0x100, 0x410001, 0)

or did I do something completely wrong?

I learn via examples, and I cant seem to find any full examples. Maybe code that take input from some text box and outputs it to notepad... Something simple for me to digest.

thanks for the responses so far.
Apr 24 '07 #10

RedSon
Expert 5K+
P: 5,000
Actually its better to use PostMessage since it returns right away.

Expand|Select|Wrap|Line Numbers
  1. LRESULT PostMessage( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
  2.  
  3. WM_KEYDOWN
  4.        WPARAM wParam
  5.         LPARAM lParam;
  6. Parameters
  7.     wParam
  8.         Specifies the virtual-key code of the nonsystem key. 
  9.     lParam
  10.         Specifies the repeat count, scan code, extended-key flag, 
  11.         context code, previous key-state flag, and transition-state flag, 
  12.         as shown in this table.
  13.  
  14. ......So......
  15.  
  16. LRESULT debugvalue = PostMessage( Handle_to_where_the_message_is_going,
  17.                                   WM_KEYDOWN, (WPARAM)VK_ENTER, (LPARAM)0)
  18.  
Apr 24 '07 #11

RedSon
Expert 5K+
P: 5,000
Keep in mind that you might also want to send a WM_KEYUP message too.
Apr 24 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.