I have a Winforms application, which hosts two PDF Viewers (Unmanaged C++) in its panels.
I want to have application wide hotkeys for project handling (Open,Save,Close,…) which would work even when users focus is on the hosted PDF Viewer.
I managed to achieve that via Winapi and RegisterHotKey, but client doesnt like that those are system wide (e.g. they disable the same MS Word hotkeys)
I did try to disable and enable global hotkeys on Form’s Activate/Deactivate events, however those events appear even when i dont leave my own app, for example on top-menu entry or on a dialog-box.
Are there other solutions for application-wide hotkeys, that would work even if the focus is on the hosted application?
SOLUTION:
In the main Form, I handle Activated and Deactivate events like this :
public void PDFPicker_Activated(object sender, EventArgs e)
{
var foregroundHwnd = GUI.winapiwrap.GetForegroundWindow();
if (foregroundHwnd==this.Handle || this.OwnedForms.Any(form => form.Handle==foregroundHwnd))
{
if (!this.hotkeys.Any())
{
registerHotkeys();
this.Text = "Hotkeys just registered";
}
}
}
public void PDFPicker_Deactivate(object sender, EventArgs e)
{
var foregroundHwnd = GUI.winapiwrap.GetForegroundWindow();
if (foregroundHwnd == this.Handle || this.OwnedForms.Any(form => form.Handle == foregroundHwnd))
{
}
else
{
unregisterHotkeys();
this.Text = "Hotkeys off";
}
}
Then in the dialog, which is in the OwnedForms array, i call just this:
private void QuestionPostingPanel_Activated(object sender, EventArgs e)
{
parent.PDFPicker_Activated(sender, e);
}
private void QuestionPostingPanel_Deactivate(object sender, EventArgs e)
{
parent.PDFPicker_Deactivate(sender, e);
}
In case of more such dialogs, I would inherit AbstractDialog : Form, add those 2 event functions and a reference to parent, and then inherit all of them from this AbstractDialog.
While registering/unregistering GlobalHotkeys, it was a problem for me when I tried doing that in parallel – global hot keys were bound to a thread.
So then check on Form Activate/Deactivate events whether the form is a foreground window or not (
::GetForegroundWindow()) and then disable/enable hotkeys appropriately.