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.
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:
And maybe a few others, but no luck. Does anyone know which command might work?
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.