dede
dede

Reputation: 11

KeyPress not being detected

My code is supposed to record keystrokes while another program runs - For example, when I press the "Q" key while in a game, my code should mark down what time I did so. Later on, I can look inside the log to see when the keystroke occured. (I am using .Net 4.5 framework.)

The keyPress is not being detected in my code below - How can I fix this?

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

Upvotes: 1

Views: 159

Answers (1)

user2480047
user2480047

Reputation:

Here you have a sample code showing you how to hook when a key is being pressed:

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

You have to evolve this (simple) code to include as many keys as you wish. The proceeding is quite easy:

  1. Call RegisterHotKey for each key you want to hook. Bear in mind that you have also the option to hook combinations (CTRL+key, CTRL+SHIFT+key, etc.), I let a comment showing how to do that.
  2. The code reached when the key is pressed is inside the condition where I wrote "'Reached every...". If you have more than one key, bear in mind that any press of any (registered) key will reach there and thus you have to know which one is every time. You can do that via m.LParam (each key has a different value for this property), you can do it manually (hardcoding the value you see for each key) or do some research (will not take you too long).

Upvotes: 1

Related Questions