【问题标题】:How do I detect keypresses in VB.NET?如何在 VB.NET 中检测按键?
【发布时间】:2009-05-22 17:55:24
【问题描述】:

如何让 VB 不断检查按键?
逐步调试时会检测到按键,但仅此而已:(
到目前为止,这是我的按键代码:

Private Sub Form_Main_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
    'Keypress Q, A, or Z if the picture is seen for the first time
    'Otherwise one accuracy point is deducted
    If e.KeyChar = Chr(97) Then 'key a
        If PictureBox.Visible = True Then
            If MainArray(X) = 1 Then
                Timer_End = TimeOfDay.Millisecond
                PictureBox.Image.Dispose()
                PictureBox.Image = Nothing
                PictureBox.Visible = False
                Accuracy = Accuracy + 1
                CalcTime()
            Else
                Timer_End = TimeOfDay.Millisecond
                PictureBox.Image.Dispose()
                PictureBox.Image = Nothing
                PictureBox.Visible = False
                Accuracy = Accuracy - 1
                CalcTime()
            End If
        End If
    End If

    'Keypress for second occurance
    If e.KeyChar = Chr(108) Then 'key l
        If PictureBox.Visible = True Then
            If MainArray(X) = 2 Then
                Timer_End = TimeOfDay.Millisecond
                PictureBox.Image.Dispose()
                PictureBox.Image = Nothing
                PictureBox.Visible = False
                Accuracy = Accuracy + 1
                CalcTime()
            Else
                Timer_End = TimeOfDay.Millisecond
                PictureBox.Image.Dispose()
                PictureBox.Image = Nothing
                PictureBox.Visible = False
                Accuracy = Accuracy - 1
                CalcTime()
            End If
        End If
    End If
End Sub

我尝试将其添加到代码中,但没有帮助:

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Do While PictureBox.Visible = True
        Application.DoEvents()
    Loop
    End Sub

【问题讨论】:

    标签: vb.net keypress


    【解决方案1】:

    Form.KeyPress 应该在每次按下一个键时触发。无论您是否正在调试,都应该调用您的事件处理程序。也许它被调用了,但它并没有按照您的预期进行?尝试放置一些调试输出语句以确认正在调用事件处理程序。

    【讨论】:

      【解决方案2】:

      此链接可能会有所帮助:

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

      还应该有通过 KeyEventArgs 类检测某些按键的示例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-10
        • 1970-01-01
        • 1970-01-01
        • 2014-03-25
        • 2014-07-27
        • 2013-12-14
        相关资源
        最近更新 更多