10

After a lot of research on Stackoverflow and google, it seems that it's difficult to send a combination of keystroke to a background window using it's handle. For example, I want to send CTRL + F. It seems that Sendmessage doesn't work, and sendinput isn't effective because the window needs the focus.

So the my last thought is about hooking: is there anyway to use that way to send combination?

Patrick
  • 17,669
  • 6
  • 70
  • 85
Louisbob
  • 860
  • 3
  • 9
  • 22

2 Answers2

8

Ok I found a workaround, but it doesn't work for all applications. Otherwise, it works with puTTY, the program I wanted to control with keystroke combination. And it works even if the application isn't focused. So I'm done now!

class SendMessage
{
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

public static void sendKeystroke()
{
    const uint WM_KEYDOWN = 0x100;
    const uint WM_KEYUP = 0x0101;

    IntPtr hWnd;
    string processName = "putty";
    Process[] processList = Process.GetProcesses();

    foreach (Process P in processList)
    {
        if (P.ProcessName.Equals(processName))
        {
            IntPtr edit = P.MainWindowHandle;
            PostMessage(edit, WM_KEYDOWN, (IntPtr)(Keys.Control), IntPtr.Zero);
            PostMessage(edit, WM_KEYDOWN, (IntPtr)(Keys.A), IntPtr.Zero);
            PostMessage(edit, WM_KEYUP, (IntPtr)(Keys.Control), IntPtr.Zero);
        }
    }                           
}

}
Louisbob
  • 860
  • 3
  • 9
  • 22
6

I've written a couple of programs that send keystrokes to background windows, I generally implemented PostMessage/SendMessage. I documented all my findings here!

But you will basically be using a low level c call to put messages into the windows message queue to allow the application to pick up the key presses.

PostMessage

SendMessage

Please let me know if you have any questions, my library is written in C# and i'd be happy to share it. This method also allows for mouse use in a background window :)

All code was checked into GitHub: https://github.com/EasyAsABC123/Keyboard

abc123
  • 17,855
  • 7
  • 52
  • 82
  • I'm playing some mobile games on BlueStacks at the moment and your library is exactly what I need. I would love to try it. Thanks for sharing! – Shinigamae Dec 31 '15 at 12:34
  • @Shinigamae never a problem, if you have any questions feel free to ask – abc123 Dec 31 '15 at 18:25
  • @abc123 it seems the background one doesn't work. Or I did implement it in wrong way. I'm using it inside an WPF application. Otherwise, the foreground one (your example) is working great. – Shinigamae Jan 05 '16 at 09:12
  • @Shinigamae simply log it as an issue, i'll likely need to know what example you need. – abc123 Jan 05 '16 at 19:33
  • @abc123 I really love your project and its excactly what I need. Currently I am using Powershell to send keys to my bluestacks game but that makes my other windows loose focus. I want my bot to do its work while I work normally on my pc. Sadly I can't get your Project itself to work neither your methods. Its not throwing an error but simply nothing happens. Any Idea how I can work something out? – Nico Feb 02 '16 at 08:13
  • 1
    @Nico it simply uses Windows Messaging, which Bluestacks might ignore. The simple solution is to inject into the Bluestacks simulator and then have that send keys to bluestacks and have that injected item have an api for keyboard from windows – abc123 Feb 03 '16 at 18:46
  • Thanks for being open to questions. I am having problem sending mouse events to move camera in a game. I am sending mouse messages with to game window. Every other control works e.g. keyboard, mouse click somewhere after `Esc` key which releases mouse and makes it visible. I am only unable to move camera with Post/Send message. In game initially mouse is invisible and used to move camera but `esc` key releases the mouse and pauses the game. In both modes mouse click works fine. But mouse move works only in released mouse mode. Do you have any suggestion about that? – SMUsamaShah Feb 23 '16 at 09:35
  • @lifeh2o so mouse_movement isn't working at all? They might be ignoring the button or...more likely is that in the sendmessage it isn't sending key down...movement....key up – abc123 Feb 23 '16 at 18:50
  • No keypress is needed to move camera. Just move mouse for the camera. When Esc is pressed game calls `ReleaseCapture` , otherwise `SetCapture` is called many times. Both [API Monitor](http://www.rohitab.com/apimonitor) and Spy++ show WM_MOUSEMOVE being received by the game but nothing happens. Game: [Dragon Prophet](http://www.dragonspropheteurope.com) – SMUsamaShah Feb 23 '16 at 19:05
  • @lifeh2o so just moving the mouse moves the camera? Try moving small increments instead of large increments (ie 1 pixel every 10ms) rather than 100 pixels left (instantly) – abc123 Feb 23 '16 at 21:10