Starting application from background process - c#

I'm working a small WPF based program for launching applications through system wide hotkeys implemented using hooking. I'm implementing it in C# and Visual Studio 2010.
When I detect the specific keypress I use Process.Start(...) to run the application. This works fine while Visual Studio is active, placing the new application in the foreground with input focus, as I would expect. If my launcher is in the background (behind another active program), it still detects the key and starts the application correctly, in front of everything else.
The problem is, that when I run the launcher without Visual Studio active, and my launcher application isn't front, neither will applications it starts. They appear in front of the launcher but behind the active application.
I can see that other software, like AutoHotkey, is able to do hotkey launching with this behavior, but I fail to see what I'm doing wrong.
Update: Just figured out a solution to this issue that works in my development environment. I first register a global hotkey through the Windows API RegisterHotKey using the launcher main window handle. The key could be any, but should be one that normally doesn't exist physically, F24 in my case. Then, whenever I detect a keypress through the hooks that should launch an app, I first call keybd_event to 'fake' a keypress for the hotkey.
For WPF use:
keybd_event((byte)KeyInterop.VirtualKeyFromKey(Key.F24),0,KEYEVENTF_KEYUP,0);
For WinForms use:
keybd_event((byte)Keys.F24,0,KEYEVENTF_KEYUP,0);
This will bring enough focus to the launcher, so that Process.Start(...) makes the executed program get in front. It does not bring the launcher window to front, nor does it make the launcher accept inputs.
If Activate() is called on the main window after the keybd_event(), this will bring the main window to front and allow for keyboard input, just as if the user had task switched.

Related

How to create a console application that runs in background?

I created app that open every time that I start pc. So its so annoying to close it every time so I'm wondering if its some code that will hide my console app. I saw videos and tutorials on forms but idk how to do it with console app.
The easiest way to do this is change your console app to a windows app.
Console apps get a console made for them by Windows. But if you change it to a windows forms app, then windows expect the application to make a window, so if you never make a window, then it will never show
The other way is to turn your application into a service. This has some additional requirements in terms of programming
Option 1
You can use this run command:
start /min "SomeTitle" MyConsoleApp.exe myarg1 myarg2
Thus it will be on the taskbar minimized.
Option 2
If you use a file link in the start menu, select the start minimized option for the exe.
Option 3
Using a WinForm app you will be able to use a tray icon by setting the main form as not visible, to say it simply because it can be complex according to the expected behavior, and it will not be in the taskbar too.
Option 4
If you don't want a main form, create a win form app, delete the form file and the code in the main method, and you're done, without GUI nor console, no main input and no main output but you can show MessageBox and some forms when necessary, just a background process only visible in the Task Manager.
With that you can add a tray icon to to offer exit and some status information for example.
Option 5
Also you can create a windows service:
.NET console application as Windows service
Note
In all cases, if you don't use an internal message events dispatcher like the WinForms Application pattern or WPF and so on, be carefull to not saturate the CPU with the processings like with loops and use Thread.Sleep() between iterations or any thread idleing pattern or some timer if necessary.

How to distinguish between a program’s GUI and Console window?

I have written a code in c# that gets processes by name, activates the window of the desired process and sends some hotkeys to this process. The problem is that the desired process has two windows, a console window and a GUI window, as a result sometimes the GUI window is activated and everything works fine, but sometimes the console window is activated and nothing happens. So my question is, is there a way to distinguish between that process’ GUI and console window, so by program won’t get confused and only activate the GUI window ?
(By activating I mean bringing this process’ window to the foreground in order for the sending hotkeys to work. I have no access to the code of the desired process so I cannot change the way it starts up).

C# How to send a hotkey globally (needed for a background process, running in system tray)

I am writing a program to automate some work for me.
I have another program (OBS Studio) installed, which I use to capture video. OBS Studio is minimized to the system tray and listens to my configured hotkey CTRL + 1.
When I press CTRL + 1 anywhere, the software starts recording. It does not matter which application is in the foreground.
I am trying to do the same thing from my application, send a "global" hotkey. I have spend hours trying to achieve this, but without any result. SendKeys only works for the current open window (which does not exist), no results with PostMessage either and I tried the wrapper "InputSimulator"
So, to summarize:
Is it possible to send a hotkey/keystroke globally (for every application?)
If not, how would I send a hotkey to a program that is running in the background, without a window? I don't want to bring the app to the foreground.
Hopefully someone with a deeper understanding of these concepts can guide me...
I've found similar questions, but they remain unanswered:
Send global keystroke / fake a global hotkey from a Winforms application
Sources I studied (among others):
https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys(v=vs.110).aspx
https://dailyoverdose.wordpress.com/2009/10/28/postmessage-and-sendmessage/
https://ourcodeworld.com/articles/read/520/simulating-keypress-in-the-right-way-using-inputsimulator-with-csharp-in-winforms

Reading characters in a background application

Hi I'm trying to make a simple program that read keys from keyboard even if my application is running in the background.
Situation
I want make a timer to help me in a game. I already have the program with the timer, the problem is I can not start the timer without switching of the game window to my app window. So I configured the game keyboard to release the keys F11, F12. Now in game this keys do nothing.
Problem
I built a windows forms containing a listener for keydown event and a conditional for F11. But when I trigger another window (eg the game window) my application no longer hears the keyboard, cuz it's in the background.
Question
How can I build a app that hears the keyboard, even if it's not active window?
You need to install a global, low-level keyboard hook using the SetWindowHookEx API call. Using the WH_KEYBOARD_LL hook will set your application up to intercept keyboard events at all times, even when your application is not active.
This post on MSDN shows an example of how to achieve something close to what you want from C#.

Prevent hotkey from being lost after a UAC prompt in Windows

BACKGROUND
I wrote an app in C# that registers windows hotkeys (it takes a screenshot when I press PRINTSCREEN)
My code to register the hotkey, capture screen , etc all works. No issues there.
THE PROBLEM
IF my app is running (and successfully has registered the hotkey) and then I do something that causes a UAC prompt - usually this is caused when, for example, I launch some setup program - then the hotkey is lost. Lost = afterwards when I hit printscreen the event never comes back to my app. I have to close the app and restart.
THE QUESTIONS
Why is the hotkey lost?
What can and should I be doing different differently when registering the hotkey?
NOTES
I've seen this behavior with other applications that register hotkeys - running the app with the hotkey using admin privileges prevents the hotkey from being lost
What can and should I be doing different differently when registering the hotkey?
Avoiding apps like setup.exes that cause the UAC prompt is not a viable solution - the entire purpose of my taking screenshots is to document a setup experience over a number of applications
I have a workaround in that I can launch the app with admin rights - but I would rather avoid having to do make users do that
CODE
My code is very similar to this: http://www.codekeep.net/snippets/ea68475b-c33e-4a71-8008-90f63d7e918d.aspx
Given that Print Screen already takes a screenshot and puts it on the clipboard, have you considered monitoring the clipboard for changes instead?
Look at the SetClipboardViewer API function. For example.
When you do something that may cause an UAC - why not unregister the hotkey first and then register it again afterwards?

Categories

Resources