2

I'm trying to send a key to an application. I tested the Handlewindow value used breakpoints to understand what I'm doing wirong but i cant find a solution. To be more detailed, its a little game and when I activate the chatbar ingame, the key I want to send will be written there, but I want to make it functionaly when I am playing to use the commands. The game doesnt have a guard or some protections.

Here is my code:

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

    const uint WM_KEYDOWN = 0x0100;

    private void button1_Click(object sender, EventArgs e)
    {
        string pName = textBox1.Text;


        //Get Processes
        Process[] processes = Process.GetProcessesByName(pName);

        //Main part
        foreach (Process p in processes)
            if (p.ProcessName == (string)pName)
            {
                    PostMessage(p.MainWindowHandle, WM_KEYDOWN, (int)Keys.W, 0);
            }


    }

Like I said, it can be sent 1000000 times successfully but nothing happens. Is there another way how I can send keys to an Windows application that works minimized or even hidden? It should be only send to my app.

Noli
  • 604
  • 2
  • 10
  • 27
  • 1
    Possible duplicate of [Simulating Key Press c#](https://stackoverflow.com/questions/3047375/simulating-key-press-c-sharp) – Deniz Jun 15 '18 at 09:20

5 Answers5

1

You can't fake messages like this and expect it to work reliably - all you are doing is sending a message, nothing fancy.

What this means is that if the target program tests for input in a different way (for example by directly checking the key state, reacting to different messages or just using a completely different mechanism) the target program at best may just completely ignore your input and at worst get totally confused.

Use the SendInput function instead (I'm not sure if this exposed as part of the .Net framework or not).

Some interesting blog articles which are fairly relevant:

Justin
  • 84,773
  • 49
  • 224
  • 367
  • I also tried it via WM_CHAR still doesnt work. Like i said, when I open the ingame Chat, and the bursor is in the like i can write there. – Noli Aug 03 '11 at 14:47
  • @Noli Try SendInput (I guess that you will need to make the game the foreground window) – Justin Aug 03 '11 at 14:52
  • Could you be so nice and help me. I dont understand Sendinput, its much more complicated, there is not just keyboardinput and I didn't find a way to use it at ohne specific handle. – Noli Aug 03 '11 at 18:13
1

If i understand correctly, you want to send keys to a game. In this case i think that the game is not fetching the keys from the Windows Message queue, it is probably reading the keyboard status directly using DirectX or similar ways.

See this similar question:

Send Key Strokes to Games

Community
  • 1
  • 1
Zmaster
  • 1,095
  • 9
  • 23
  • 1
    Wow thats realy much for me, my skills are not realy high. Hooking API in C# is too hard for me. Anyway thanks a lot I keep understanding it! – Noli Aug 03 '11 at 15:09
1

Just note that the correct import of PostMessage is:

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

Sending the keys like you are doing might not work, but you can use spy++ to capture the actual messages that are being posted to the application by windows and use those.

https://learn.microsoft.com/en-us/visualstudio/debugger/introducing-spy-increment?view=vs-2022

critic
  • 309
  • 2
  • 4
0

Often programmers will react to KeyUp events rather than down; did you try sending WM_KEYUP?

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
0

You should be using SendInput to simulate input into another window.

See this blog post for more details.

dlev
  • 48,024
  • 5
  • 125
  • 132