Click Counter

  • Thread starter Thread starter Drew
  • Start date Start date
D

Drew

I would like to find out how to build a click counter using VB.NET. This is
not a run-of-the-mill click counter (i.e. for web use), but a novelty app
that counts how many times you click *anything* with your mouse (left or
right).

I play Rise of Nations and after a battle, the stats include a Speed stat,
which counts how many times the mouse was clicked, how many hotkeys were
used, etc. I think it would be neat to find out how many times I click my
mouse everyday.

I currently develop web apps using ASP (classic) and VBScript, but would
like to try to work on this on my off-time. I am completely new to VB.NET,
but would like to learn, and this may be a good way to do so.

Any advice on where to begin?

Thanks,
Drew
 
I have figured this out with some help. This is done by using a
mouse/keyboard hook and it works well.

Here is some code, make sure that you close the form instead of hitting the
Stop button. If you hit the stop button, the mouse hook stays and you must
reboot.

' --------------------------------------------------
' Form1
' --------------------------------------------------
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents leftClicksLabel As System.Windows.Forms.Label
Friend WithEvents rightClicksLabel As System.Windows.Forms.Label
Friend WithEvents keyPressesLabel As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.leftClicksLabel = New System.Windows.Forms.Label
Me.rightClicksLabel = New System.Windows.Forms.Label
Me.keyPressesLabel = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'leftClicksLabel
'
Me.leftClicksLabel.Anchor =
CType(((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right),
System.Windows.Forms.AnchorStyles)
Me.leftClicksLabel.Location = New System.Drawing.Point(8, 8)
Me.leftClicksLabel.Name = "leftClicksLabel"
Me.leftClicksLabel.Size = New System.Drawing.Size(280, 24)
Me.leftClicksLabel.TabIndex = 0
Me.leftClicksLabel.Text = "LeftClicks:"
Me.leftClicksLabel.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft
'
'rightClicksLabel
'
Me.rightClicksLabel.Anchor =
CType(((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right),
System.Windows.Forms.AnchorStyles)
Me.rightClicksLabel.Location = New System.Drawing.Point(8, 40)
Me.rightClicksLabel.Name = "rightClicksLabel"
Me.rightClicksLabel.Size = New System.Drawing.Size(280, 24)
Me.rightClicksLabel.TabIndex = 1
Me.rightClicksLabel.Text = "RightClicks:"
Me.rightClicksLabel.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft
'
'keyPressesLabel
'
Me.keyPressesLabel.Anchor =
CType(((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right),
System.Windows.Forms.AnchorStyles)
Me.keyPressesLabel.Location = New System.Drawing.Point(6, 72)
Me.keyPressesLabel.Name = "keyPressesLabel"
Me.keyPressesLabel.Size = New System.Drawing.Size(280, 24)
Me.keyPressesLabel.TabIndex = 2
Me.keyPressesLabel.Text = "KeyPresses:"
Me.keyPressesLabel.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 102)
Me.Controls.Add(Me.keyPressesLabel)
Me.Controls.Add(Me.rightClicksLabel)
Me.Controls.Add(Me.leftClicksLabel)
Me.Name = "Form1"
Me.Text = "Form1"
Me.TopMost = True
Me.ResumeLayout(False)

End Sub

#End Region

Private leftClicks As Integer
Private rightClicks As Integer
Private keyPresses As Integer

Private WithEvents llm As New LowLevelMouseHook
Private WithEvents llkb As New LowLevelKeyBoardhook

Private Sub llm_LeftClick() Handles llm.LeftClick
leftClicks = leftClicks + 1
leftClicksLabel.Text = "Left Clicks: " & leftClicks
End Sub

Private Sub llm_RightClick() Handles llm.RightClick
rightClicks = rightClicks + 1
rightClicksLabel.Text = "Right Clicks: " & rightClicks
End Sub

Private Sub llkb_KeyPress() Handles llkb.KeyPress
keyPresses = keyPresses + 1
keyPressesLabel.Text = "Keypresses: " & keyPresses
End Sub

End Class



' --------------------------------------------------
' Class LowLevelMouseHook
' --------------------------------------------------
Imports System.Runtime.InteropServices
Public Class LowLevelMouseHook

Private Const HC_ACTION As Integer = 0
Private Const WH_MOUSE_LL As Integer = 14
Private Const WM_LBUTTONDOWN As Integer = &H201
Private Const WM_RBUTTONDOWN As Integer = &H204

Public Structure POINT
Private x As Integer
Private y As Integer
End Structure

Public Structure MSLLHOOKSTRUCT
Private pt As POINT
Private mouseData As Integer
Private flags As Integer
Private time As Integer
Private dwExtraInfo As Integer
End Structure

Private Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" ( _
ByVal idHook As Integer, _
ByVal lpfn As LowLevelMouseProcDelegate, _
ByVal hmod As Integer, _
ByVal dwThreadId As Integer) As Integer

Private Declare Function CallNextHookEx Lib "user32" ( _
ByVal hHook As Integer, _
ByVal nCode As Integer, _
ByVal wParam As Integer, _
ByVal lParam As MSLLHOOKSTRUCT) As Integer

Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
ByVal hHook As Integer) As Integer

Private Delegate Function LowLevelMouseProcDelegate( _
ByVal nCode As Integer, _
ByVal wParam As Integer, _
ByVal lParam As MSLLHOOKSTRUCT) As Integer

Public Event LeftClick()
Public Event RightClick()

Private hhkLowLevelMouse As Integer

Public Sub New()
hhkLowLevelMouse = SetWindowsHookEx(WH_MOUSE_LL, AddressOf
Me.LowLevelMouseProc, _
Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32,
0)
End Sub

' Callback function must be Public!
Public Function LowLevelMouseProc( _
ByVal nCode As Integer, _
ByVal wParam As Integer, _
ByVal lParam As MSLLHOOKSTRUCT) As Integer

If (nCode = HC_ACTION) Then
Select Case wParam
Case WM_LBUTTONDOWN
RaiseEvent LeftClick()

Case WM_RBUTTONDOWN
RaiseEvent RightClick()

End Select
End If

Return CallNextHookEx(hhkLowLevelMouse, nCode, wParam, lParam)
End Function

Protected Overrides Sub Finalize()
UnhookWindowsHookEx(hhkLowLevelMouse)
MyBase.Finalize()
End Sub

End Class



' --------------------------------------------------
' Class LowLevelKeyBoardhook
' --------------------------------------------------
Imports System.Runtime.InteropServices
Public Class LowLevelKeyBoardhook

Private Const HC_ACTION As Integer = 0
Private Const WH_KEYBOARD_LL As Integer = 13
Private Const WM_KEYDOWN As Integer = &H100
Private Const WM_SYSKEYDOWN As Integer = &H104

Public Structure KBDLLHOOKSTRUCT
Private vkCode As Integer
Private scancode As Integer
Private flags As Integer
Private time As Integer
Private dwExtraInfo As Integer
End Structure

Private Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" ( _
ByVal idHook As Integer, _
ByVal lpfn As LowLevelKeyboardProcDelegate, _
ByVal hmod As Integer, _
ByVal dwThreadId As Integer) As Integer

Private Declare Function CallNextHookEx Lib "user32" ( _
ByVal hHook As Integer, _
ByVal nCode As Integer, _
ByVal wParam As Integer, _
ByVal lParam As KBDLLHOOKSTRUCT) As Integer

Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
ByVal hHook As Integer) As Integer

Private Delegate Function LowLevelKeyboardProcDelegate( _
ByVal nCode As Integer, _
ByVal wParam As Integer, _
ByVal lParam As KBDLLHOOKSTRUCT) As Integer

Public Event KeyPress()

Private hhkLowLevelKeyboard As Integer

Public Sub New()
hhkLowLevelKeyboard = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf
Me.LowLevelKeyboardProc, _
Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32,
0)
End Sub

' Callback function must be Public!
Public Function LowLevelKeyboardProc( _
ByVal nCode As Integer, _
ByVal wParam As Integer, _
ByVal lParam As KBDLLHOOKSTRUCT) As Integer

If (nCode = HC_ACTION) Then
Select Case wParam
Case WM_KEYDOWN, WM_SYSKEYDOWN
RaiseEvent KeyPress()

End Select
End If

Return CallNextHookEx(hhkLowLevelKeyboard, nCode, wParam, lParam)
End Function

Protected Overrides Sub Finalize()
UnhookWindowsHookEx(hhkLowLevelKeyboard)
MyBase.Finalize()
End Sub

End Class

Hope this helps future searchers!
Drew
 
Back
Top