0

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;
    }
Community
  • 1
  • 1
zeuschyant8
  • 453
  • 7
  • 14
  • _On a particular key stroke, the application comes to front_ just how is that done? You point to a link the not only has many answers but is also a duplicate. It would help to know which method you are using.. Also you should probably tell us the platform, I assume Winforms, right? – TaW Jul 31 '14 at 11:20
  • answer #5 from lupok worked for me, and yes the platform is winforms. – zeuschyant8 Jul 31 '14 at 11:23
  • Oh, sorry I gues I could have picked that up from your code ;-) Have you tried to simply add a `this.textBox_Search.Focus();` ? ATM you are putting the focus explicitly to the form.. – TaW Jul 31 '14 at 11:26
  • Yes TaW, I did try Focus(), doesn't seem to work – zeuschyant8 Jul 31 '14 at 11:30
  • Hm, putting `textBox_Search.Focus();` at the end of your code certainly set focus to the textbox. However I must say that I really can't re-create the whole setup. Where do you put your code so that it gets picked up while the program is minimized?? – TaW Jul 31 '14 at 11:55
  • TaW, thanks a lot for looking into it. I've updated my code to give some more information on how the key strokes are linked and processed. One more thing, this issue occurs only when my application is not minimized but some other application is brought to foreground and then again my application is opened using the key stroke. If my application is minimized and then brought up using key stroke, then it does get the key strokes well. – zeuschyant8 Aug 01 '14 at 03:23
  • I can't reproduce it here. I have installed the keyboard hook from [codeproject](http://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook) and using your code the testprogram comes up both from the taskbar and from behind any other programm, and the tabpage is selected and the focus is on a textbox on its 2nd page. no problems at all. I did notice a warning about which names to register for the control key(s) but that didn't make any difference. – TaW Aug 03 '14 at 10:42
  • Okay, I'll see into it again. Will post when I find the problem. Thanks again for looking into it. – zeuschyant8 Aug 03 '14 at 16:46

0 Answers0