KeyPress未被检测到

时间:2013-08-08 18:52:29

标签: .net vb.net winforms

我的代码应该在另一个程序运行时记录击键 - 例如,当我在游戏中按下“Q”键时,我的代码应该记下我什么时候这样做。稍后,我可以查看日志内部以查看击键发生的时间。 (我正在使用.Net 4.5框架。)

我的代码中未检测到keyPress - 如何解决此问题?

Public Class MainForm
    Dim startTime As DateTime

    Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "Start" Then
            startTime = DateTime.Now
            TimerLoad.Start()
            Button1.Text = "Stop"
        Else
            TimerLoad.Stop()
            Button1.Text = "Start"
            Dim FileNumber As Integer = FreeFile()
            FileOpen(FileNumber, "C:\Users\JasonValidia\Documents\MontageTimer.txt", OpenMode.Output)
            For Each Item As Object In ListBox1.Items
                PrintLine(FileNumber, Item.ToString)
            Next
            FileClose(FileNumber)
        End If
    End Sub

    Private Sub TimerLoad_Tick(sender As System.Object, e As System.EventArgs) Handles TimerLoad.Tick
        Dim timeDifferance As TimeSpan = DateTime.Now.Subtract(startTime)
        Dim newDate As DateTime = timeDifferance.ToString
        Label1.Text = newDate.ToString("HH:mm:ss")
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

        ListBox1.Items.Add(Label1.Text)

    End Sub

    Private Sub login_KeyDown(ByVal sender As Object,
                              ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        If e.KeyCode = Keys.Enter Then

            ListBox1.Items.Add(Label1.Text)
            If CheckBox1.Checked = True Then startTime = DateTime.Now
        End If
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

这里有一个示例代码,显示了在按下某个键时如何挂钩:

Imports System.Runtime.InteropServices
Public Class Form1
    Private Const WM_HOTKEY As Integer = &H312
    Private Const MOD_ALT As Integer = &H1
    Private Const MOD_CONTROL As Integer = &H2
    Private Const MOD_SHIFT As Integer = &H4
    Private Declare Function RegisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifier As Integer, ByVal vk As Integer) As Integer
    Private Declare Function UnregisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If (m.Msg = WM_HOTKEY AndAlso m.WParam = CType(0, IntPtr)) Then
                If (m.LParam = 5308416) Then
                    MessageBox.Show("You pressed Q")
                ElseIf (m.LParam = 4521984) Then
                    MessageBox.Show("You pressed E")
                End If
        End If

        MyBase.WndProc(m)
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        UnregisterHotKey(Me.Handle, 0)
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RegisterHotKey(Me.Handle, 0, Nothing, Keys.E)
        RegisterHotKey(Me.Handle, 0, Nothing, Keys.Q) 'RegisterHotKey(Me.Handle, 0, MOD_CONTROL, Keys.Q)
    End Sub
End Class

您必须改进此(简单)代码以包含任意数量的密钥。这个过程非常简单:

  1. 为要挂钩的每个键调用RegisterHotKey。请记住这一点 您还可以选择挂钩组合(CTRL +键, CTRL + SHIFT +键等),我让评论显示如何做 这一点。
  2. 按下键时到达的代码是在我写“'到达每一个......'的条件下。如果您有多个钥匙,请记住,任何(注册)钥匙的任何按键都会到达那里,因此您必须每次都知道哪一个。你可以通过m.LParam(每个键具有不同的属性值)来做到这一点,你可以手动完成(硬编码你看到的每个键的值)或做一些研究(不会花太长时间)。