views:

922

answers:

4

update: I've modified the code below to reveal some additional information regarding the key that was pressed.

update #2: I've discovered the root cause of the issue: we have an HTML control (Gecko rendering engine) on our form. When that Gecko rendering engine navigates to some Flash control, suddenly ~2% of our key presses don't get through, even after we've removed the Gecko HTML control. Wahoo, I get to blame this on Flash! Now the question is, how do I fix this?

update #3: Nope, it's not Flash. It's the Gecko rendering engine. Navigating even to Google causes some keys to not come through to our app right. Hrmmm.

We have a strange case in our WinForms app where the user presses a key combination (in this case, Alt+S), and WinForms tells us some other key combo (value 262162) is pressed:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if(keyData == (Keys.S | Keys.Alt))
    {
       Console.WriteLine("You pressed Alt+S");
    }
    else if(keyData == (Keys.Menu | Keys.Alt))
    {
       Console.WriteLine("What the hell?"); // This sometimes gets hit when I press Alt+S
    }
}

90% of the time, "You pressed Alt+S" will be shown. But on rare occurrances, we press Alt+S and it says, "What the hell?"

Any idea what's wrong?

+1  A: 

From my testing, it would appear that 262162 would be the "Alt" key.

Edit: I overrode the ProcessCmdKey and put in a break point on the "X = 1;" statement:

    protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
    {
        int x = (int)keyData;
        if (x == 262162)
            x = 1;
        return true;
    }

It hits that breakpoint whenever I hit the Alt key.

CAbbott
Console.WriteLine((int)Keys.Alt); // prints 262144
Judah Himango
Edit: Heh, you're right. So what the heck -- why isn't Alt+S working? When it doesn't work, I hear the Windows "ding" sound played, like there's no shortcut registered. Strange...what's going on?
Judah Himango
Your code would work if you eliminate your "else if" condition. You should'nt have to worry about cases where "Alt-S" isn't being pressed. The "ding" may be coming in if your keypress code is causing the buffer to overflow.
CAbbott
+1  A: 

Could it be that you are waiting too long to press the S key and an Alt key repeat is getting fired off?

EDIT2:I tried you posted code and I receive "What the Hell" the second I touch the ALT key, however if I disable this check the Alt-S always comes through. On my system this seems to be the default key code for the Alt key. I am able to ignore it and the Alt-S Will come through afterwards.

EDIT: According to the METADATA for the Keys enumeration Keys.Menu is 18. To see this hit F12 while the cursor is on the Keys Enumeration.

The documentation states that Keys.Menu is the Alt key.

http://msdn.microsoft.com/en-us/library/system.windows.forms.keys.aspx

This tells me that it is reporting the Alt key was pressed(18) with the Alt key modifier(262144) crazy stuff.

Justin
RE: *"Could it be that you are waiting too long to press the S key and an Alt key repeat is getting fired off?"* I don't think so; I tried holding down the Alt key for about 10 seconds, then hitting S. It still registered as Alt+S.
Judah Himango
Thanks for the pointer about the Menu key. Looking at var menu = Keys.Menu in the debugger, it shows the value as (Keys.Rbutton | Keys.ShiftKey).
Judah Himango
RE:"Looking at var menu = Keys.Menu in the debugger, it shows the value as (Keys.Rbutton | Keys.ShiftKey)" I see that same thing. Even if I hover over Keys.MenuAfter receiving "What the Hell" does the Alt-S eventually post?
Justin
I tried you posted code and I receive "What the Hell" the second I touch the ALT key, however if I disable this check the Alt-S always comes through.
Justin
Ok. That was pseudo code of course; the original was just checking for Alt+S, never for anything else. Occassionally, Alt+S has no effect, other than to set off the "windows ding" sound from the OS.
Judah Himango
+3  A: 
Judah Himango
You'll want to make sure to set the KeyPressEventArgs.Handled property to get the functionality you'll want. Look here for details: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx
CAbbott
Yep, I'm aware of that, thanks.
Judah Himango
Sounds similar to an old battle we had with the .NET WebBrowser and it's screwy behavior with Flash (and other things). Have fun with it, sir!
STW
A: 

Just out of curiosity could you try this:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.S | Keys.Alt))
        {
            MessageBox.Show("You pressed Alt+S");
        }
        else if (keyData == (Keys.Menu | Keys.Alt))
        {
            return false;
        }

        return true;
    }

returning false indicates that this is not a command key.

Justin