断点影响ProcessCmdKey处理keyData

时间:2009-09-12 04:26:46

标签: .net winforms keyboard

任何人都可以解释为什么在ProcessCmdKey方法中按Alt +向右箭头键触发Alt +左​​箭头键的检查?当我最初对这种方法进行编码时,一切正常。我开始质疑我所有的关键处理程序,但想知道是否有一个很好的解释或我是否遗漏了一些东西。所有其他组合键都按预期工作。

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

解决方法是检查按键为If keyData = (Keys.Alt Or Keys.Left) Then

[更新] 啊,我明白了。谢谢米奇。我做了检查,但错过了。

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

仍然想知道为什么命中断点会改变行为。

[更新] 再次谢谢米奇。由于它不能为您重现,我怀疑解决方案.suo文件已损坏。我删除了这个文件,现在点击断点没有任何影响。

1 个答案:

答案 0 :(得分:0)

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

所以按位,Keys.Right包含Key.Left。因此,And Keys.LeftTrue返回{{1}}。