Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

c# - SendInput fails to send enterkey to specific program

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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++) {

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...