表格的KeyEvent只能结合CTRL表格、KeyEvent、CTRL

由网友(苏秋辰)分享简介:code:Private Sub KeyHandling(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDownSelect Case e.KeyCodeCase Keys.LeftbtnPre...

code:

Private Sub KeyHandling(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    Select Case e.KeyCode
      Case Keys.Left
        btnPrev.PerformClick()
      Case Keys.Right
        btnNext.PerformClick()
      Case Keys.Up
        btnFirst.PerformClick()
      Case Keys.Down
        btnLast.PerformClick()
    End Select
End Sub

我的形式主要preVIEW属性已启用。

The KeyPreview property of my form is enabled.

问题:

这code什么都不会做,除非我按住Ctrl键。任何人都可以解释一下吗? :)

This code won't do anything, except when I hold the control key. Can anyone explain this? :)

推荐答案

这是因为光标键得到早期拦截,KeyDown事件触发之前。的WinForms用它来移动焦点,就像标签。当您按住Ctrl键,它不再是一个导航键,您的KeyDown事件可以看出来。

This is because the cursor keys get intercepted early, before the KeyDown event fires. Winforms uses it to move the focus, just like Tab. When you hold down the Ctrl key, it is no longer a navigating key and your KeyDown event can see it.

您通常会修复通过重写IsInputKey(),但不会工作,如果表单中存在的控件。如果设置键preVIEW为真,他们可能会做。该形式永远不会获得焦点,控件做。你需要放弃对关键preVIEW,这是一个古老的VB6不合时宜,无论如何,你通过覆盖ProcessCmdKey抓住光标键()。像这样的:

You'd normally fix that by overriding IsInputKey() but that won't work if the form has any controls. They probably do if you set KeyPreview to true. The form never gets the focus, the controls do. You need to give up on KeyPreview, it's an old VB6 anachronism anyway, you catch the cursor keys by overriding ProcessCmdKey(). Like this:

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
    If keyData = Keys.Left Then
        Console.WriteLine("left")
        Return True
    End If
    Return MyBase.ProcessCmdKey(msg, keyData)
End Function
阅读全文

相关推荐

最新文章