I have an application which sits minimized in system tray. On a particular key stroke, the application comes to front. (Thanks to How can I bring my application window to the front?)
On the main screen of the application lies a 'type to search' kind of textbox. User has to start typing in here.
However, even when my application comes to front, key strokes are still received by the background application and nothing gets typed inside the textbox.
I've tried textbox.focus(), textbox.Select(), form.activecontrol = textbox, but nothing seems to work.
Here's how my code looks:
gkh.HookedKeys.Add(Keys.Oemtilde);
gkh.HookedKeys.Add(Keys.ControlKey);
gkh.HookedKeys.Add(Keys.Escape);
gkh.KeyUp += new KeyEventHandler(gkh_KeyUp);
void gkh_KeyUp(object sender, KeyEventArgs e)
{
if ((Keys.Oemtilde == e.KeyCode) && (Keys.Control == Control.ModifierKeys))
{
// Set the WindowState to normal
this.WindowState = FormWindowState.Normal;
//Bring the main tab to display
tabControl1.SelectTab(tabPage_Search);
this.TopMost = true;
this.Focus();
this.BringToFront();
this.TopMost = false;
this.textBox_Search.Select();
this.textBox_Search.SelectionStart = 0;
this.textBox_Search.Focus();
}
else if (Keys.Escape == e.KeyCode)
{
//Minimize to system tray when Escape key is pressed.
this.WindowState = FormWindowState.Minimized;
}
else
{
//NOP - Intentionally kept empty
}
e.Handled = true;
}