I've read similar questions around this issue, including Best way to tackle global hotkey processing in C#? and Set global hotkeys using C#. I've also investigated a NuGet package Global Hotkeys which appears to be in its infancy.
The problem here is, most of them seem to be designed for Winforms, or could probably run in WPF. The P/Invoke they're using seems to require a window handle. I'm thinking of having a windoless application here, i.e running without a main form or window, except when a certain key combo is pressed, so there might not actually be a handle.
So, would passing a cheeky 0 as the Window handle for the P/Invoke cause it to not look for a window to process the keypress on? Or is my best bet here to use an invisible unfocusable Window?
To add a little context, I'm making a windowless app to be used with TTS providing the feedback for control, my target audience here are blind and visually impaired users. Occasionally, things will have to be entered, so I want to be able to launch forms when necessary, but for the most part I'd like there to be no window cluttering things up.
Some sample code (I can't verify if this would work properly yet).
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ScreenReader.sapiEnable(true);
ScreenReader.SayString("Launching application...");
// bind hotkeys here
Application.Run();
}
// called when the right keyboard shortcut is pressed
static void ExitApp()
{
ScreenReader.SayString("Exiting program");
Application.Exit();
}
Thanks for any help you may be able to provide.