Search code examples
c#winapipostmessage

Which WM_Message to use?


I am working on a client loader for a game not made by me. I've built a library of mouse and key events to send to the client and they work for the most part. However, there is one bit I cannot seem to figure out.

I made the typing portion of my code this:

[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);

//Send each character of string one at a time
foreach(Char c in Input)
    PostMessage(mainwnd, (uint)WM.CHAR, c, 1);
//Send final enter key to send message in game
PostMessage(mainwnd, (uint)WM.CHAR, 0x0D, 1);

This works fine, but there are two things it doesn't account for.

  1. In-Game you are required to press the enter key before typing to open chat
  2. There are hotkeys to open menus

I have tried things like PostMessage(mainwnd, (uint)WM.CHAR, 0x0D, 1); to send the beginning enter key, but the game doesn't process it. Same thing happens if I use PostMessage to try to open menus. Nothing is parsed unless the chat window is already open before sending the message.

I figure I am using the wrong WM_Message, but I cannot find which one I need. I have tried:

  1. WM_CHAR
  2. WM_SYSCHAR
  3. WM_MENUCHAR
  4. WM_HOTKEY
  5. WM_APPCOMMAND
  6. WM_KEYDOWN followed by WM_KEYUP

And maybe a few others, but no luck. Does anyone know which command might work?


Solution

  • You will have to get lucky for this to work. Keyboard input is not merely a message. It also affects (among others) GetKeyboardState and GetAsyncKeyState -- and simulating input with these is non-trivial. And if keyboard focus is elsewhere, it can screw things up.

    You can try posting WM_KEYDOWN and WM_KEYUP to generate a slightly lower-level message.

    Lower-level still is SendInput which simulates input from the keyboard driver. Keyboard focus must be in the right place or it will send the input to the wrong window.