0

I am trying to inject sendkeys into the active desktop application, however am finding that I loose the active desktop application focus to the application I'm building. And thus application is a fail.

The application I'm building should never gain focus over the active window when run via the keypress.

EG.

  1. Keypress mapped to run app I'm building, eg app.exe "key 1"
  2. App then injects send keys for typing out "key 1" to the active application or uses windows copy, sets buffer to "key 1" and then sends CTRL+V to the active application in focus.

I've tried creating a console app but gains focus of itself.

Then I tried a windows service however I then realised I needed to build another app to send the command to the service which will again gain focus.

Suggestions?

Some code I used for getting active process: Always gets itself as it becoms the active app, so quite pointless.

````

class Program
{
    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

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

    static void Main(string[] args)
    {
        IntPtr handle = GetForegroundWindow();

        Process p = Process.GetCurrentProcess();
        Debug.Print("p.ProcessName = " + p.ProcessName);

        String msg = "";
        msg += "p.ProcessName = " + p.ProcessName.ToString() + "\n";

}

````

Darcey
  • 1,949
  • 1
  • 13
  • 22
  • Please show the code you're trying. Is it anything like this: https://stackoverflow.com/questions/15292175/c-sharp-using-sendkey-function-to-send-a-key-to-another-application. Also your question is a little unclear. – Rufus L Jun 29 '18 at 16:24
  • (update your **Question** with a code sample, not the **comments** section) – Rufus L Jun 29 '18 at 16:26
  • You need some kind of telekinesis to get the program started. And *not* create a console window, [that is very easy to do](https://stackoverflow.com/a/2686476/17034). – Hans Passant Jun 29 '18 at 17:42

1 Answers1

0

Spoooooky, I 'solved' this one today while spending a free hour adding some remote monitoring to a factory terminal.

Make a regular winforms project, set the forms WindowState to minimized and ShowInTaskbar to false. Then on the form.load event call hide()

Now you have an invisible program that can't be manually targetted, now you just need to make sure it's targetting the window you want. Which is done like this in VB (you're a big boy, translate it online by yourself):

'in the form class def
Declare Auto Function FindWindow Lib "User32.dll" (ByVal lpClassName As String,ByVal lpWindowName As String) As IntPtr
Declare Auto Funtion SetForeGroundWindow Lib "User32.dll"(ByVal Hwnd As IntPtr) As Long

Sub blah()
    Dim Handle as IntPtr = FindWindow(Nothing,"ClockWatcher")
    SetForeGroundWindow(Handle)
End Sub

Which is when I would use windows.forms.sendkeys(thething) to simulate the keypresses. Good thing is, focus remains on the clockwatch program, and nothing the user can do will get focus onto your form. Unless you make a taskbar icon, they have to use taskmgr to stop the program :P

Davesoft
  • 724
  • 1
  • 4
  • 10
  • Ooo didn't try that option, seemed a bit dirty :p lol... Issue is the window can be dynamic, could be anything. I have no idea what program could be in focus when the program is called on. I need to be able to run something that doesn't take application focus away from what is active at the moment. I have an idea but it requires 2 applications (1 service which and then the app which requests info from the service at init). Service basically polls active applications and on run of send keys app, it requests from the service what was the app in focus before it was run. So polling req'. – Darcey Jun 29 '18 at 19:51
  • PS. Also found a solution using AutoHotkey, but ran into other issues later down the line, which C# wouldn't have issue with. – Darcey Jun 29 '18 at 19:54
  • Sounds like this might help you https://code.msdn.microsoft.com/windowsapps/How-to-get-the-title-of-4ec7f32f – Davesoft Jun 29 '18 at 20:33
  • Also, consider running the program at startup, and using another method of telling it to Go, like a file existing on the filesystem, which it then deletes before begining the Go. Running a fresh program without getting focus is possible, but you'll need to be the .net program doing the launching, since it's the launch parameters that can make it not-focused. – Davesoft Jun 29 '18 at 20:37