Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending keys to a game

Tags:

c#

.net

sendkeys

So I'm stuck with a problem, I'm trying to send keys to a game and I have the game in the foreground with help of SetForegroundWindow and I'm using SendInputs API to send the keys to the game.

If I focus on another application the keys are sent to that application but as soon as I focus on the application I want the keys to be sent to, they don't appear there.

I'm trying to save me some time to recruit guild members for my guild and with that I'm trying to send keys to the game.

 [DllImport("user32.dll")]
 private static extern bool SetForegroundWindow(IntPtr hWnd);

 [DllImport("user32.dll")]
 static extern IntPtr GetMessageExtraInfo();

 [DllImport("user32.dll", SetLastError = true)]
 static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

 Process[] procs = Process.GetProcessesByName("BlackDesert64");
 if (procs.Length > 0)
 {
   if (procs[0].MainWindowHandle != IntPtr.Zero)
   {
      SetForegroundWindow(procs[0].MainWindowHandle);
      Thread.Sleep(1000);
   }
 }
 INPUT[] inputs = new INPUT[]
 {
     new INPUT
     {
         type = INPUT_KEYBOARD,
         u = new InputUnion
         {
             ki = new KEYBDINPUT
             {
                 wVk = 0x49,
                 wScan = 0049,
                 dwFlags = KEYEVENTF_UNICODE,
                 dwExtraInfo = GetMessageExtraInfo(),
             }
         }
     },
     new INPUT
     {
         type = INPUT_KEYBOARD,
         u = new InputUnion
         {
             ki = new KEYBDINPUT
             {
                 wVk = 0x49,
                 wScan = 0049,
                 dwFlags = KEYEVENTF_KEYUP,
                 dwExtraInfo = GetMessageExtraInfo(),
             }
         }
    }
};

SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));

Rest of the code: https://pastebin.com/RUm7A311

UPDATE

So I've found the API Interceptor that allows to send keys to a game that uses DirectX and I've set it up but still no outcome.. anyone who can point me in the right direction?

like image 932
Tweath Avatar asked Mar 30 '17 19:03

Tweath


2 Answers

What does value SendInput return?

If it returns 0, then its an indication that some error has happened. You can try to invoke GetLastError, to see if the input was blocked by the UIPI, alternatively try to run your code with local administrator privileges.

Are you sure that procs[0].MainWindowHandle is the correct window handle?

Lastly try to send the message directly to the handle using SendMessage.

like image 105
Rasmus Søborg Avatar answered Nov 19 '22 03:11

Rasmus Søborg


Implementation using SendMessage (no need to focus on the window).

[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("User32.dll")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindows);

[DllImport("User32.dll")]
private static extern Int32 SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, StringBuilder lParam);

void SendKeys()
{
    IntPtr hWnd = FindWindow("Notepad", "Untitled - Notepad");
    if (!hWnd.Equals(IntPtr.Zero))
    {
        IntPtr edithWnd = FindWindowEx(hWnd, IntPtr.Zero, "Edit", null);
        if (!edithWnd.Equals(IntPtr.Zero))
        {
            SendMessage(edithWnd, WM_SETTEXT, IntPtr.Zero, new StringBuilder("Test"));
        }
    }
}

Reference: how-do-i-input-to-another-application

like image 1
Antony Avatar answered Nov 19 '22 02:11

Antony