ctusch

Hi,

I have a form which consists of a webBrowser control (over the whole form) and a listBox and a button, but those 2 are hidden on start. I'm using them for debugging and I want to toggle their visibility with a keyboard shortcut (alt+ctrl+X in my case). So I'm using the keyDown event of the form and keyPreview is set to true. Everything works as intended until I click somewhere into the webBrowser control (i.e. it gains the focus). I've tried alot of things, like using the gotFocus event on the webBrowser and then using this.Focus() to give the focus back to the form, but nothing work. It seems like the webBrowser control doesn't want to give the focus back.. I already did alot of googleing and also searched here but I haven't found any solution which addresses exactly my problem. So can anyone give me a hint on how to take the focus away from the webBrwoser control Any answer is appreciated!

Regards


Re: Windows Forms General problems with the webBrowser control and focus

RizwanSharp

What is happening that you have placed KeyDown on Form but not on Browser so whenever Browser gets focus these events are missed. The better way is to capture the keys before they are actually reached to the desired control.

try this code in your form:

Declare 2 contacts in your form

private const int WM_KEYDOWN = 0x100;

private const int WM_SYSKEYDOWN = 0x104;

And override ProcessCmdKey function of Form class like this:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
if ((keyData) == (Keys.Control | Keys.Alt | Keys.X)) // If control + alt + delete is pressed simultainously
{
MessageBox.Show("You Pressed Ctrl + Alt + X"); // Or Do your work Here
}

}
return base.ProcessCmdKey(ref msg, keyData);
}

I hope this will help.

Best Regards,

Rizwan aka RizwanSharp






Re: Windows Forms General problems with the webBrowser control and focus

ctusch

Hello Rizwan,

first off thanks for the fast and helpful reply. Your solution solves the problem with not getting the focus but that doesn't seem to be the only problem. Now everything works like before as long as the webBrowser control doesn't gain the focus. As soon as it does, instead of doing nothing as before, now my listBox flashes up for maybe 50ms every time I hit ctrl+alt+X and then disappears again. It seems like the webBrowser control is very... well dominant I'd say. I've already tried .BringToFront() and .Update() on my listBox (in the ProcessCmdKey method), but nothing changes. Have you (or anybody else) got an idea what's the deal with the webBrowser control

Regards




Re: Windows Forms General problems with the webBrowser control and focus

ctusch

I've just found out something very interessting: as soon as the webBrowser control gains focus, everytime I press ctrl+alt+X the ProccessCmdKey method is called 2 (!) times! As long as it doesn't have focus just one time (as it should). So that's why it flashes. One solution would be to also toggle the webBrowser's visibility, so it loses focus, though you have to press ctrl+alt+X 2 times in order to get the listBox (assuming the webBrowser has focus). Another (a bit better) solution would be to introduce a global boolean variable which you toggle with each ctrl+alt+x and if that boolean is set to true you toggle the visibility of the listbox. Though, when another control than the webBrowser control has focus it wouldn't work every 2nd time, because the method is just called 1 time. So both solutions are very ugly and I'm really wondering what's wrong with the webBrowser control. Is it some kind of hook (just random guessing) I hope someone can help me!

EDIT:
Ok now this solution works as intended, but as I've said above it's somehow ugly:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
    if(msg.Msg == WM_KEYDOWN || msg.Msg == WM_SYSKEYDOWN) {
        //If control + alt + X is pressed simultainously the visibility of the debugging controls toggles
        if(keyData == (Keys.Control | Keys.Alt | Keys.X)) {
            log.Visible = !log.Visible;
            saveLog.Visible = !saveLog.Visible;

            if(webBrowser1.Focused)
                toggle = !toggle;
                if(toggle) {
                    log.Visible = !log.Visible;
                    saveLog.Visible = !saveLog.Visible;
                }
        }
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

Just for explenation: log is my listBox saveLog is a button and toggle is a global boolean variable.