6

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?

Tweath
  • 124
  • 6
  • 24
  • You could try to get AutoHotKey to work with the game. I once did that and it took me a while until it worked, because some methods apparently don't work for certain games for various reasons. Maybe those are the same reasons your program is not working. It could help you narrow down the problem: https://autohotkey.com/docs/commands/Send.htm – Sentry Apr 01 '17 at 21:07
  • Try to use the [sendkeys.Send](https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx) method – itay_421 Apr 02 '17 at 08:39
  • Already tried that method. @itay_421 – Tweath Apr 02 '17 at 14:04
  • Have you tried keybd_event? example here: http://stackoverflow.com/questions/14395377/how-to-simulate-a-ctrl-a-ctrl-c-using-keybd-event msdn: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx – Eric Dahlvang Apr 04 '17 at 10:10

3 Answers3

4

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.

Rasmus Søborg
  • 3,597
  • 4
  • 29
  • 46
1

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

Antony
  • 1,221
  • 1
  • 11
  • 18
0

For your problem here is trick,

Use

String Keys = "Test";
SendKeys.Send(Keys);

this code to send keys to any application.

Just put this code in timer_start() add some delay before starting of timer and stop timer after execution.

Now run your project which will initiate timer, before timeout open your game and wait for keys to press!!

Check this link which contains all Keys and their code to send https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx

Vijay Thorat
  • 98
  • 11