473,387 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Click Counter

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
Nov 21 '05 #1
1 7503
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.Ge tExecutingAssembly.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.Ge tExecutingAssembly.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
"Drew" <dr********@NOswvtc.dmhmrsas.virginia.SPMgov> wrote in message
news:O9*************@tk2msftngp13.phx.gbl...
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

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Mike Mimic | last post by:
Hi! I am trying to implement link click counter so that user can still bookmark the link (I rather see that click is not counted than user have "problems"). <a href="http://www.google.com/"...
2
by: Pete | last post by:
I have some funky form/event behavior. Access 97. Split frontend/backend, using Access security. I have the same behavior (or lack of behavior) for the pag_Click() event of two separate pages...
0
by: Drew | last post by:
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...
1
by: beihotmailanmelden | last post by:
Hello, here's the situation: I have a banner on a third party page that, if you click on it, links to another third party page. I cannot install a counter on that thirdparty page. Is there...
0
by: Diogo Alves - Software Developer | last post by:
Hi I have a busy dialog (i.e. a form with a progress bar with the description of what is happening in the moment) but when I have a counter on it to show how many events have been processed...
6
by: Jim Devenish | last post by:
I have an unbound form that displays all the days of the year as a calendar. It has 12 rows of text boxes with either 29,30 or 31 in each row. Text box names are of the form: display_01_01,...
1
by: 12600 | last post by:
Iam new and learning Java script and can't find any good sources and slow at learning. I want this button to not disable so i can click it numerous time and display my counter number of howmany...
3
by: yimma216 | last post by:
I hava a form, which has five lines to enter at start. I want to have a button to add more lines when it is clicked. However, I am having trouble with it. This would be a form in a form using vbscipt...
14
by: Ed Jay | last post by:
Despite searching the net, I can't find a suitable solution that prevents a user's double click from submitting a form twice. I'm currently using the following in a button element: <input...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.