Hi All,
I need to create a program that can run code each time the user clicks, I can't use windows forums since its on another program. If the code can detect global mouse clicks(system wide) or only on one program it's fine.
I've done some digging and it looks like windows API can solve my problem but i really suck at using windows API and it looks like SetWindowsHookEx() can solve my problem but i don't know how to use it correctly.
info on the windows API
pinvoke.net: SetWindowsHookEx (user32)
Using Hooks (Windows)
SetWindowsHookEx Function (Windows)
This is the code from Pinvoke that I modified to hopefully work for a mouse click. i'm having trouble getting this to work.
any ideas on how to get this working for a mouse click?
Thanks in advance
I need to create a program that can run code each time the user clicks, I can't use windows forums since its on another program. If the code can detect global mouse clicks(system wide) or only on one program it's fine.
I've done some digging and it looks like windows API can solve my problem but i really suck at using windows API and it looks like SetWindowsHookEx() can solve my problem but i don't know how to use it correctly.
info on the windows API
pinvoke.net: SetWindowsHookEx (user32)
Using Hooks (Windows)
SetWindowsHookEx Function (Windows)
This is the code from Pinvoke that I modified to hopefully work for a mouse click. i'm having trouble getting this to work.
VB.NET:
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Module Module1
Sub Main()
Dim t As New MouseHook
Console.ReadLine()
End Sub
Public Class MouseHook
Delegate Function HookProc(ByVal code As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
Private myCallbackDelegate As HookProc = Nothing
Public Sub New()
' initialize our delegate
Me.myCallbackDelegate = New HookProc(AddressOf MyCallbackFunction)
' setup a Mouse hook
SetWindowsHookEx(7, myCallbackDelegate, IntPtr.Zero, AppDomain.GetCurrentThreadId)
End Sub
<DllImport("user32.dll")> _
Friend Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As HookProc, ByVal hInstance As IntPtr, ByVal threadId As Integer) As Integer
End Function
<DllImport("user32.dll")> _
Friend Shared Function CallNextHookEx(ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
Private Function MyCallbackFunction(ByVal code As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
MsgBox("something happened!!")
Return CallNextHookEx(IntPtr.Zero, code, wParam, lParam)
End Function
End Class
End Module
Thanks in advance