3

I'm making a program that can send mouse click, keyboard input and strings to game windows (even if the window is inactive).

I use SendMessage to send mouse click and keyboard input, it works fine.

But when I try to send strings to window, I found that it can't send the entire string at the same time (only send one char each time).

Here's the code I'm using:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern uint SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

public static void sendString(IntPtr hWnd, string s)
{
    foreach (char c in s)
    {
        SendMessage(hWnd, (int)WM_CHAR, (int)c, 0);
    }
}

private void button1_Click(object sender, EventArgs e)
{
    sendString(handle, "中文 string");
}

It works like:

中
中文
中文 
中文 s
中文 st

...and so on

but what I expect is to send "中文 string" at the same time.

I've tried:

SendMessage(handle, WM_SETTEXT, 0, "中文 string");

but it changes the window title (the game is a DirectX application, and the textbox does not have its own handle).

and also tried:

SendKeys.Send("中文 string");

or Douglas's answer, but those only works when the window is actived.

I've googled for many answers and still can't find the best way, and I don't want to use copy & paste.

So if there is a way to send entire string at the same time to window that is inactive by using C#?

(I saw someone made a program by using EYuYan(易語言), and his tool can achieve my desired effect, so I think there must be a solution for C#)

Community
  • 1
  • 1
Lay
  • 356
  • 2
  • 11
  • Use WM_SETTEXT could only work if the handle is a child EDIT control. But you already know it is not, nothing unusual in DirectX games. There is nothing wrong with a for() loop, do what works. – Hans Passant Nov 08 '15 at 16:55
  • [You can't simulate keyboard input with PostMessage](http://blogs.msdn.com/b/oldnewthing/archive/2005/05/30/423202.aspx). – IInspectable Nov 08 '15 at 19:43

0 Answers0