1

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.

Zeeebass
  • 37
  • 8
  • **make the enter key press a bit longer** if`*SendKeys.SendWait* no longer works make your own implentation. Look at the Ref Source Code for that method and see if you can adjust it to overcome the games change. – Jeremy Thompson Jan 25 '21 at 00:28
  • First off all, I am a beginner in c# I don’t think I can adjust the method so it will work... – Zeeebass Jan 25 '21 at 01:01
  • If you download ILSpy and then open the System.Winfows.Form.DLL then view the underlying implementation for the Microsoft SendKEys.SendWait method. I'm saying to make your own implementation based of theirs that can work around the game preventing the Enter key and/or Waiting a second. – Jeremy Thompson Jan 25 '21 at 01:03
  • You replied quick! Haha I googled and I found it, I don’t think it would be able to. Sendkeys doesn’t work at all anymore and having to change a whole method seems difficult to me. – Zeeebass Jan 25 '21 at 01:06
  • Pro tip: my motto as a developer is "go after it; like a dog after a bone". Often a working solution is just 5 more minutes away. Try giving this a go: https://stackoverflow.com/a/20493025/495455 or https://stackoverflow.com/a/20484836/495455 – Jeremy Thompson Jan 25 '21 at 01:46
  • The stackoverflow answers you told me to give a go. I already used in my code. These are fully working... https://stackoverflow.com/a/48967155/9127583 I am using his Send(a); method... – Zeeebass Jan 25 '21 at 08:03
  • I like the tip tho, I have to admit, I never expected me to make it even this far into my project in a few days hahaha – Zeeebass Jan 25 '21 at 08:04

1 Answers1

1

This questions says you have to add a delay

Using two (ugly) Thread.sleep here the game sees the enter and it works fine.

        Thread.Sleep(100);
        Send(ScanCodeShort.RETURN);
        Thread.Sleep(100);

Going to test with the amout of delay. I hope i can reduce it even lower. Because my winForm uses timers on the form....

Edit:

To make the operation more concise I iterate over the alphabet like this:

for (char c = 'A'; c <= 'Z'; c++) {
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
Zeeebass
  • 37
  • 8
  • Thanks Jeremy for your edit.I did some research but I don’t seem the understand what the code is for. I reckon I have to change it with the for each. But I don’t see how that would benefit... – Zeeebass Jan 26 '21 at 00:49