I created a program that is able to send text to the chat in a game.
The game had an update, before I used Sendkeys.SendWait("My text");
Sadly, this no longer works...
After a while, I found this stackoverflow question. Using SendInput, the answer of this question made it work except for the enter key that activates the chatbox.
My code activates with a button press, then it will select the right process.
public void BringMainWindowToFront(string processName)
{
Process bProcess = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(processName)).FirstOrDefault();
if (bProcess != null)
{
if (bProcess.MainWindowHandle == IntPtr.Zero)
{
ShowWindow(bProcess.Handle, ShowWindowEnum.Restore);
}
SetForegroundWindow(bProcess.MainWindowHandle);
}
}
After that I call TypeAll and this will type all the characters to the chat.
private void TypeAll(string text)
{
IDictionary<string, string> lettersToScanCode = new Dictionary<string, string>();
lettersToScanCode.Add("a", "KEY_A"); //This Dictionary translates the inputkeys to
the exact enum.
lettersToScanCode.Add("b", "KEY_B");
lettersToScanCode.Add("c", "KEY_C");
lettersToScanCode.Add("d", "KEY_D");
lettersToScanCode.Add("e", "KEY_E");
lettersToScanCode.Add("f", "KEY_F");
lettersToScanCode.Add("g", "KEY_G");
lettersToScanCode.Add("h", "KEY_H");
lettersToScanCode.Add("i", "KEY_I");
lettersToScanCode.Add("j", "KEY_J");
lettersToScanCode.Add("k", "KEY_K");
lettersToScanCode.Add("l", "KEY_L");
lettersToScanCode.Add("m", "KEY_M");
lettersToScanCode.Add("n", "KEY_N");
lettersToScanCode.Add("o", "KEY_O");
lettersToScanCode.Add("p", "KEY_P");
lettersToScanCode.Add("q", "KEY_Q");
lettersToScanCode.Add("r", "KEY_R");
lettersToScanCode.Add("s", "KEY_S");
lettersToScanCode.Add("t", "KEY_T");
lettersToScanCode.Add("u", "KEY_U");
lettersToScanCode.Add("v", "KEY_V");
lettersToScanCode.Add("w", "KEY_W");
lettersToScanCode.Add("x", "KEY_X");
lettersToScanCode.Add("y", "KEY_y");
lettersToScanCode.Add("z", "KEY_Z");
lettersToScanCode.Add(":", "SEPARATOR");
lettersToScanCode.Add(" ", "SPACE");
lettersToScanCode.Add("0", "KEY_0");
lettersToScanCode.Add("1", "KEY_1");
lettersToScanCode.Add("2", "KEY_2");
lettersToScanCode.Add("3", "KEY_3");
lettersToScanCode.Add("4", "KEY_4");
lettersToScanCode.Add("5", "KEY_5");
lettersToScanCode.Add("6", "KEY_6");
lettersToScanCode.Add("7", "KEY_7");
lettersToScanCode.Add("8", "KEY_8");
lettersToScanCode.Add("9", "KEY_9");
Send(ScanCodeShort.RETURN); //Here I send the enter key so I open the chatbox
char[] characters = text.ToCharArray(); //This foreach will send all the text.
foreach (char i in characters)
{
string newI = i.ToString().ToLower();
string str = lettersToScanCode[newI];
Enum.TryParse(str, out ScanCodeShort test);
Send(test);
}
}
Is there another way to call the enter key?
Or make the enter key press a bit longer, so the game actually sees it's getting pressed.
question from:
https://stackoverflow.com/questions/65877459/sendinput-fails-to-send-enterkey-to-specific-program