1

我的代码应该在另一个程序运行时记录击键 - 例如,当我在游戏中按下“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
4

1 回答 1

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(每个键对此属性具有不同的值)来执行此操作,您可以手动进行(硬编码您为每个键看到的值)或进行一些研究(不会花费您太长时间)。
于 2013-08-08T19:20:00.997 回答