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.
- Keypress mapped to run app I'm building, eg app.exe "key 1"
- 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";
}
````