views:

127

answers:

1

Can anyone explain why the check for Alt+Left Arrow key is triggered on a Alt+Right Arrow key press within the ProcessCmdKey method? When I originally coded this method, everything worked. I am beginning to question all my key handlers, but wanted to know if there was a good explaination or if I am missing something. All other key combinations work as expected.

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
    If (keyData And Keys.Alt) = Keys.Alt Then
        If (keyData And Keys.Left) = Keys.Left Then
            'when Alt+Right key is pressed
            '   this executes, except when a breakpoint is set anywhere within this method
            '   this still executes in released code
            Debug.WriteLine("WTF!")
        End If
    End If
    Return MyBase.ProcessCmdKey(msg, keyData)
End Function

The work around is to check for the key press as If keyData = (Keys.Alt Or Keys.Left) Then

[update] Ah, I got it. Thank you Mitch. I did check for that, but missed it.

? convert.ToString(Keys.Left, 2)
"100101"
? convert.ToString(Keys.Right,2)
"100111"

Still would like to know why hitting the breakpoint changes behavior.

[update] Thank you again Mitch. Since it wasn't reproducible for you, I suspect the solutions .suo file is corrupt. I deleted this file and now hitting the breakpoint has no affect.

A: 
Keys.Left  = 37 = 32 + 4     + 1
Keys.Right = 39 = 32 + 4 + 2 + 1

So bitwise, Keys.Right includes Key.Left. Therefore Anding with Keys.Left return True in either case.

Mitch Wheat
I checked that. Why does it work properly when a breakpoint is set and execution stops?
AMissico
It didn't when I tried that code...
Mitch Wheat
...but you have your reason why the code above isn't working as expected...
Mitch Wheat
Hmm, not reproducible on your computer, so it makes me wonder if the solutions .suo file is corrupt. I deleted .suo and now hitting the breakpoint has no affect.
AMissico
@AMissico: that's interesting. I had no idea a problem with the .suo file could have such an effect...
Mitch Wheat
Yes, interesting. It is something that just hit me to do because earlier I suggested to someone else to delete their .suo to fixed a different problem, and it worked. So I thought...breakpoints, weird behavior...delete .suo.
AMissico