Results 1 to 14 of 14

Thread: [RESOLVED]Capturing mouse click event in vb.net(Thanks so much Mr.Polite)

  1. #1

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    [RESOLVED]Capturing mouse click event in vb.net(Thanks so much Mr.Polite)

    Hello..
    Can someone help on how I can detect the mousedown,mouseup etc even outside my application using vb.net?
    I have an API here,but,doesnt seem to work the way I expect it to..
    VB Code:
    1. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    2.  
    3.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    4.         If GetAsyncKeyState(1) = "1" Then
    5.             Label1.Text = ("Left mouse button clicked")
    6.         Else
    7.             Label1.Text = "mouse button up"
    8.         End If
    9.     End Sub
    Should that API be modified to be used or something?? or what should I do to capture the mouse click events..both left and right buttons...
    Thanks
    Last edited by uniquegodwin; Sep 4th, 2005 at 08:51 AM.
    Godwin

    Help someone else with what someone helped you!

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Capturing mouse click event in vb.net

    hey it's not the answer, but take a look at the codeproject page here.... I was reading on MSDN about assigning a global mouse hook and it says that you cant have global hooks in .NET (I think a global mouse hook would let you recieve a mouse click event without you having to check the mouse state in a timer like that )

    anyways this page says the same but has a solution. I think this is what you want keep in mind that using hooks can be a bit risky, cuz if you mess up your whole app will vanish (ie, crash) I'm not so familiar with APIs but this seems like a better solution

    also I looked over at it, and when the form closes it doesn't remove the hook (it has to call the Stop function of that helper class... look at the code). If you ask a API guru I think they'd tell you that it's a better idea to unhook it before closing your form? I'm still not sure, someone needs to confirm this (does the hook need to be removed before the app exists?)

    so many frogs so little time

    edit: the frogs distracted me and I forgot the link
    http://www.codeproject.com/csharp/globalhook.asp


    edit #2: the frogs made me think the world knows C#. Let me know if you need help converting it to VB

    edit #3: there are 9 frogs in this post, count for yourself
    Last edited by MrPolite; Sep 4th, 2005 at 12:08 AM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3
    New Member
    Join Date
    Sep 2005
    Posts
    4

    Re: Capturing mouse click event in vb.net

    Thanks Thanks Thanks Mr Polite....
    Yes,I do need help converting to vb.net if possible...Please help if u can,I would be really grateful.
    Thanks again so much,..
    And about the frogs,there r 10 frogs in the post totally

  4. #4
    New Member
    Join Date
    Sep 2005
    Posts
    4

    Re: Capturing mouse click event in vb.net

    WOW,This code project link you gave me...That c# app is EXCELLENT...It works sooo perrrfecttlyyy when I execute it...
    I tried searching on google on detecting mouse clicks...but never found a solution like this I wonder how u found it.It is Awesomeee...I will try figuring out to convert it into vb.net on my own and if Im not,Ill ask for help,please give me a hand if possible at that time...Thanks Mr.Polite

  5. #5
    New Member
    Join Date
    Sep 2005
    Posts
    4

    Re: Capturing mouse click event in vb.net

    oops,never realised my classmates id was logged on here..ohh noo..i was posting thinking its my id...nevermind anyway..Its me Godwin right now

  6. #6
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Capturing mouse click event in vb.net

    Quote Originally Posted by virtual_manu
    Thanks Thanks Thanks Mr Polite....
    Yes,I do need help converting to vb.net if possible...Please help if u can,I would be really grateful.
    Thanks again so much,..
    And about the frogs,there r 10 frogs in the post totally
    haha 10 including my sig
    you got me so confused, I thought someone else wanted this same thing too lol

    if you don't already have it, download Reflector
    it's more like a decompiler, but in this case, if you try to decompile that codeproject sample to VB, it's a good tool for converting from C# to vb.net


    minutes later...
    listen, it turns out that my VB skills are fading away I copy pasted a lot of code from the decompiler and did everything that I could just to make it compile. I STRONGLY descourage you from using it,. it's most likely unsatble. I couldnt even find a nice way to add the event handlers in. ugh
    the C# version isnt all that nice either. the guy uses classes where he could use structures (for the api calls)
    for reference, in the next two posts

    I didn't even double check ANYTHING. it's a horrible mess that I'm posting. I'm posting it in case you had trouble converting something from c# to vb... it compiles and the click events seem to work fine. But it won't detect mouse move, and the keyboard keys seem to get lost...
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Capturing mouse click event in vb.net

    VB Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class UserActivityHook
    4.     Public Delegate Function HookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
    5.  
    6.     Public KeyboardHookProcedure As HookProc
    7.     Public KeyDown As KeyEventHandler
    8.     Public KeyPress As KeyPressEventHandler
    9.     Public KeyUp As KeyEventHandler
    10.  
    11.     Public OnMouseActivity As MouseEventHandler
    12.  
    13.  
    14.     Private Shared hKeyboardHook As Integer
    15.     Private Shared hMouseHook As Integer
    16.     Public MouseHookProcedure As HookProc
    17.     Public Const WH_KEYBOARD_LL As Integer = 13
    18.     Public Const WH_MOUSE_LL As Integer = 14
    19.     Private Const WM_KEYDOWN As Integer = 256
    20.     Private Const WM_KEYUP As Integer = 257
    21.     Private Const WM_LBUTTONDBLCLK As Integer = 515
    22.     Private Const WM_LBUTTONDOWN As Integer = 513
    23.     Private Const WM_LBUTTONUP As Integer = 514
    24.     Private Const WM_MBUTTONDBLCLK As Integer = 521
    25.     Private Const WM_MBUTTONDOWN As Integer = 519
    26.     Private Const WM_MBUTTONUP As Integer = 520
    27.     Private Const WM_MOUSEMOVE As Integer = 512
    28.     Private Const WM_RBUTTONDBLCLK As Integer = 518
    29.     Private Const WM_RBUTTONDOWN As Integer = 516
    30.     Private Const WM_RBUTTONUP As Integer = 517
    31.     Private Const WM_SYSKEYDOWN As Integer = 260
    32.     Private Const WM_SYSKEYUP As Integer = 261
    33.  
    34. #Region "dll imports"
    35.     ' Methods
    36.     <DllImport("user32.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Auto)> _
    37.     Public Shared Function CallNextHookEx(ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
    38.     End Function
    39.  
    40.     <DllImport("user32")> _
    41.     Public Shared Function GetKeyboardState(ByVal pbKeyState As Byte()) As Integer
    42.     End Function
    43.  
    44.  
    45.  
    46.     <DllImport("user32.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Auto)> _
    47.     Public Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As HookProc, ByVal hInstance As IntPtr, ByVal threadId As Integer) As Integer
    48.     End Function
    49.  
    50.     <DllImport("user32")> _
    51.     Public Shared Function ToAscii(ByVal uVirtKey As Integer, ByVal uScanCode As Integer, ByVal lpbKeyState As Byte(), ByVal lpwTransKey As Byte(), ByVal fuState As Integer) As Integer
    52.     End Function
    53.  
    54.     <DllImport("user32.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Auto)> _
    55.     Public Shared Function UnhookWindowsHookEx(ByVal idHook As Integer) As Boolean
    56.     End Function
    57. #End Region
    58.  
    59. #Region "Structures and whatnot"
    60.     <StructLayout(LayoutKind.Sequential)> _
    61.      Public Class KeyboardHookStruct
    62.         ' Fields
    63.         Public dwExtraInfo As Integer
    64.         Public flags As Integer
    65.         Public scanCode As Integer
    66.         Public time As Integer
    67.         Public vkCode As Integer
    68.     End Class
    69.  
    70.     <StructLayout(LayoutKind.Sequential)> _
    71.     Public Class MouseHookStruct
    72.         ' Fields
    73.         Public dwExtraInfo As Integer
    74.         Public hwnd As Integer
    75.         Public pt As POINT
    76.         Public wHitTestCode As Integer
    77.     End Class
    78.  
    79.     <StructLayout(LayoutKind.Sequential)> _
    80.     Public Class POINT
    81.         ' Fields
    82.         Public x As Integer
    83.         Public y As Integer
    84.     End Class
    85. #End Region
    86.  
    87.  
    88.  
    89.  
    90.     Public Sub New()
    91.         Start()
    92.     End Sub
    93.  
    94.     Protected Overrides Sub Finalize()
    95.         Try
    96.             Me.Stop()
    97.         Finally
    98.             MyBase.Finalize()
    99.         End Try
    100.     End Sub
    101.  
    102.  
    103.  
    104.  
    105.  
    106.     Public Sub Start()
    107.         If (UserActivityHook.hMouseHook = 0) Then
    108.             Me.MouseHookProcedure = New HookProc(AddressOf Me.MouseHookProc)
    109.             UserActivityHook.hMouseHook = UserActivityHook.SetWindowsHookEx(14, Me.MouseHookProcedure, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)), 0)
    110.             If (UserActivityHook.hMouseHook = 0) Then
    111.                 Me.Stop()
    112.                 Throw New Exception("SetWindowsHookEx failed.")
    113.             End If
    114.         End If
    115.         If (UserActivityHook.hKeyboardHook = 0) Then
    116.             Me.KeyboardHookProcedure = New HookProc(AddressOf Me.KeyboardHookProc)
    117.             UserActivityHook.hKeyboardHook = UserActivityHook.SetWindowsHookEx(13, Me.KeyboardHookProcedure, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)), 0)
    118.             If (UserActivityHook.hKeyboardHook = 0) Then
    119.                 Me.Stop()
    120.                 Throw New Exception("SetWindowsHookEx ist failed.")
    121.             End If
    122.         End If
    123.     End Sub
    124.  
    125.     Public Sub [Stop]()
    126.         Dim flag1 As Boolean = True
    127.         Dim flag2 As Boolean = True
    128.         If (UserActivityHook.hMouseHook <> 0) Then
    129.             flag1 = UserActivityHook.UnhookWindowsHookEx(UserActivityHook.hMouseHook)
    130.             UserActivityHook.hMouseHook = 0
    131.         End If
    132.         If (UserActivityHook.hKeyboardHook <> 0) Then
    133.             flag2 = UserActivityHook.UnhookWindowsHookEx(UserActivityHook.hKeyboardHook)
    134.             UserActivityHook.hKeyboardHook = 0
    135.         End If
    136.         If (Not flag1 OrElse Not flag2) Then
    137.             Throw New Exception("UnhookWindowsHookEx failed.")
    138.         End If
    139.     End Sub
    140.  
    141.  
    142.     Private Function MouseHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
    143.         If ((nCode >= 0) AndAlso (Not Me.OnMouseActivity Is Nothing)) Then
    144.             Dim buttons1 As MouseButtons = MouseButtons.None
    145.             Dim num3 As Integer = wParam
    146.             If (num3 <> 513) Then
    147.                 If (num3 = 516) Then
    148.                     buttons1 = MouseButtons.Right
    149.                 End If
    150.             Else
    151.                 buttons1 = MouseButtons.Left
    152.             End If
    153.             Dim num1 As Integer = 0
    154.             If (buttons1 <> MouseButtons.None) Then
    155.                 If ((wParam = 515) OrElse (wParam = 518)) Then
    156.                     num1 = 2
    157.                 Else
    158.                     num1 = 1
    159.                 End If
    160.             End If
    161.             Dim struct1 As MouseHookStruct = CType(Marshal.PtrToStructure(lParam, GetType(MouseHookStruct)), MouseHookStruct)
    162.             Dim args1 As New MouseEventArgs(buttons1, num1, struct1.pt.x, struct1.pt.y, 0)
    163.             Me.OnMouseActivity.Invoke(Me, args1)
    164.         End If
    165.         Return UserActivityHook.CallNextHookEx(UserActivityHook.hMouseHook, nCode, wParam, lParam)
    166.     End Function
    167.  
    168.  
    169.     Private Function KeyboardHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
    170.         If ((nCode >= 0) AndAlso (((Not Me.KeyDown Is Nothing) OrElse (Not Me.KeyUp Is Nothing)) OrElse (Not Me.KeyPress Is Nothing))) Then
    171.             Dim struct1 As KeyboardHookStruct = CType(Marshal.PtrToStructure(lParam, GetType(KeyboardHookStruct)), KeyboardHookStruct)
    172.             If ((Not Me.KeyDown Is Nothing) AndAlso ((wParam = 256) OrElse (wParam = 260))) Then
    173.                 Dim args1 As New KeyEventArgs(CType(struct1.vkCode, Keys))
    174.                 Me.KeyDown.Invoke(Me, args1)
    175.             End If
    176.             If ((Not Me.KeyPress Is Nothing) AndAlso (wParam = 256)) Then
    177.                 Dim buffer1 As Byte() = New Byte(256 - 1) {}
    178.                 UserActivityHook.GetKeyboardState(buffer1)
    179.                 Dim buffer2 As Byte() = New Byte(2 - 1) {}
    180.                 If (UserActivityHook.ToAscii(struct1.vkCode, struct1.scanCode, buffer1, buffer2, struct1.flags) = 1) Then
    181.                     Dim args2 As New KeyPressEventArgs(Convert.ToChar(buffer2(0)))
    182.                     Me.KeyPress.Invoke(Me, args2)
    183.                 End If
    184.             End If
    185.             If ((Not Me.KeyUp Is Nothing) AndAlso ((wParam = 257) OrElse (wParam = 261))) Then
    186.                 Dim args3 As New KeyEventArgs(CType(struct1.vkCode, Keys))
    187.                 Me.KeyUp.Invoke(Me, args3)
    188.             End If
    189.         End If
    190.         Return UserActivityHook.CallNextHookEx(UserActivityHook.hKeyboardHook, nCode, wParam, lParam)
    191.     End Function
    192.  
    193. End Class
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  8. #8
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Capturing mouse click event in vb.net

    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.  
    4. #Region " Windows Form Designer generated code "
    5.  
    6.     Public Sub New()
    7.         MyBase.New()
    8.  
    9.         'This call is required by the Windows Form Designer.
    10.         InitializeComponent()
    11.  
    12.         'Add any initialization after the InitializeComponent() call
    13.  
    14.     End Sub
    15.  
    16.     'Form overrides dispose to clean up the component list.
    17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    18.         If disposing Then
    19.             If Not (components Is Nothing) Then
    20.                 components.Dispose()
    21.             End If
    22.         End If
    23.         MyBase.Dispose(disposing)
    24.     End Sub
    25.  
    26.     'Required by the Windows Form Designer
    27.     Private components As System.ComponentModel.IContainer
    28.  
    29.     'NOTE: The following procedure is required by the Windows Form Designer
    30.     'It can be modified using the Windows Form Designer.  
    31.     'Do not modify it using the code editor.
    32.     Friend WithEvents textBox As System.Windows.Forms.TextBox
    33.     Friend WithEvents labelMousePosition As System.Windows.Forms.Label
    34.     Friend WithEvents buttonStop As System.Windows.Forms.Button
    35.     Friend WithEvents buttonStart As System.Windows.Forms.Button
    36.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    37.         Me.textBox = New System.Windows.Forms.TextBox
    38.         Me.labelMousePosition = New System.Windows.Forms.Label
    39.         Me.buttonStop = New System.Windows.Forms.Button
    40.         Me.buttonStart = New System.Windows.Forms.Button
    41.         Me.SuspendLayout()
    42.         '
    43.         'textBox
    44.         '
    45.         Me.textBox.Font = New System.Drawing.Font("Courier New", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World)
    46.         Me.textBox.Location = New System.Drawing.Point(8, 126)
    47.         Me.textBox.Multiline = True
    48.         Me.textBox.Name = "textBox"
    49.         Me.textBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
    50.         Me.textBox.Size = New System.Drawing.Size(280, 240)
    51.         Me.textBox.TabIndex = 7
    52.         Me.textBox.Text = ""
    53.         '
    54.         'labelMousePosition
    55.         '
    56.         Me.labelMousePosition.Location = New System.Drawing.Point(40, 94)
    57.         Me.labelMousePosition.Name = "labelMousePosition"
    58.         Me.labelMousePosition.Size = New System.Drawing.Size(184, 23)
    59.         Me.labelMousePosition.TabIndex = 6
    60.         Me.labelMousePosition.Text = "labelMousePosition"
    61.         '
    62.         'buttonStop
    63.         '
    64.         Me.buttonStop.Location = New System.Drawing.Point(144, 46)
    65.         Me.buttonStop.Name = "buttonStop"
    66.         Me.buttonStop.TabIndex = 5
    67.         Me.buttonStop.Text = "Stop"
    68.         '
    69.         'buttonStart
    70.         '
    71.         Me.buttonStart.Location = New System.Drawing.Point(40, 46)
    72.         Me.buttonStart.Name = "buttonStart"
    73.         Me.buttonStart.TabIndex = 4
    74.         Me.buttonStart.Text = "Start"
    75.         '
    76.         'Form1
    77.         '
    78.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    79.         Me.ClientSize = New System.Drawing.Size(296, 413)
    80.         Me.Controls.Add(Me.textBox)
    81.         Me.Controls.Add(Me.labelMousePosition)
    82.         Me.Controls.Add(Me.buttonStop)
    83.         Me.Controls.Add(Me.buttonStart)
    84.         Me.Name = "Form1"
    85.         Me.Text = "Form1"
    86.         Me.ResumeLayout(False)
    87.  
    88.     End Sub
    89.  
    90. #End Region
    91.  
    92.     Private actHook As UserActivityHook
    93.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    94.         Me.actHook = New UserActivityHook
    95.         Me.actHook.OnMouseActivity = New MouseEventHandler(AddressOf MouseMoved)
    96.         Me.actHook.KeyDown = New KeyEventHandler(AddressOf MyKeyDown)
    97.         Me.actHook.KeyPress = New KeyPressEventHandler(AddressOf MyKeyPress)
    98.         Me.actHook.KeyUp = New KeyEventHandler(AddressOf MyKeyUp)
    99.     End Sub
    100.  
    101.     Private Sub buttonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonStart.Click
    102.         actHook.Start()
    103.     End Sub
    104.  
    105.     Private Sub buttonStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonStop.Click
    106.         actHook.Stop()
    107.     End Sub
    108.  
    109.     Public Sub MouseMoved(ByVal sender As Object, ByVal e As MouseEventArgs)
    110.         Me.labelMousePosition.Text = String.Format("x={0}  y={1}", e.X, e.Y)
    111.         If (e.Clicks > 0) Then
    112.             Me.LogWrite(("MouseButton " & ChrW(9) & "- " & e.Button.ToString))
    113.         End If
    114.     End Sub
    115.  
    116.  
    117.     Public Sub MyKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
    118.         Me.LogWrite(("KeyDown " & ChrW(9) & "- " & e.KeyData.ToString))
    119.     End Sub
    120.  
    121.  
    122.     Public Sub MyKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
    123.         Me.LogWrite(("KeyPress " & ChrW(9) & "- " & e.KeyChar))
    124.     End Sub
    125.  
    126.  
    127.     Public Sub MyKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
    128.         Me.LogWrite(("KeyUp " & ChrW(9) & ChrW(9) & "- " & e.KeyData.ToString))
    129.     End Sub
    130.  
    131.  
    132.  
    133.  
    134.     Private Sub LogWrite(ByVal txt As String)
    135.         Me.textBox.AppendText((txt & Environment.NewLine))
    136.         Me.textBox.SelectionStart = Me.textBox.Text.Length
    137.     End Sub
    138. End Class
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  9. #9

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Re: Capturing mouse click event in vb.net

    Mr.Polite,Thank you so so so so sooo muchhhh...
    The detecting click was the main thing.Im trying to make an open source remote desktop application..its opensource..but,all source isnt mine though..i took some code from the internet too to help me with it I wonder if I have to ask their permission to make it opensource.
    Never mind the mouse move Mr.Polite ..
    for the mouse move,I was already using this concept to get it...
    VB Code:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2.         Me.Text = Cursor.Position.X & "x" & Cursor.Position.Y
    3.     End Sub
    Thanks again Mr.Polite I did do a little bit reading on the windows hook thing,but it was about some kind of messages passed between windows or something and theres some point there which we control? something like that,i understood something..im not clear how exactly we are able to detect mouse using this windows hook.Ill still read again and further and try to understand.Thank you so much Mr.Polite
    Godwin

    Help someone else with what someone helped you!

  10. #10
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [RESOLVED]Capturing mouse click event in vb.net(Thanks so much Mr.Polite)

    Mr.Polite

    is there a way to use that class you posted to have a timer be enabled only with the left mouse button is pressed?

    ie: if the user holds down the left mouse button (even outside of this application) a timer is going, but as soon as the left mouse button is no longer being pressed the timer is set to false?

  11. #11
    Junior Member
    Join Date
    Jul 2006
    Location
    Warner Robins GA
    Posts
    27

    Re: [RESOLVED]Capturing mouse click event in vb.net(Thanks so much Mr.Polite)

    Conctact me if you need help with Hooking please... I have written and taught how to perform Low Level Hooks in VB.NET and C# and can explain clearly the entire process as well as distribute professionally written code.

  12. #12
    New Member
    Join Date
    Dec 2009
    Posts
    1

    Re: [RESOLVED]Capturing mouse click event in vb.net(Thanks so much Mr.Polite)

    I am a beginner in VB.net and I cannot figure out how to do a mouseclick hook.


    I know how to do it on forms but I get totally lost on the API and other stuff.


    If someone can please walk me through the process step by step I would really appreciate it. My yahoo messenger id is x_shakespear_x

    Or you can post it up here as a tutorial, because I know I cannot be the only one that is totally lost over the whole Hook scenario.

  13. #13
    New Member
    Join Date
    Nov 2010
    Posts
    2

    Re: [RESOLVED]Capturing mouse click event in vb.net(Thanks so much Mr.Polite)

    Sorry for reopening a resolved post. I have tried running the above code. Unfortunately it runs into the exception on line 112 in the UserActivityHook class.


    Code:
    Throw New Exception("SetWindowsHookEx failed.")

    I look back the code and found out that on line 109


    Code:
    UserActivityHook.hMouseHook = UserActivityHook.SetWindowsHookEx(14, Me.MouseHookProcedure, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)), 0)
    the function SetWindowsHookEx does not return a value and when i look back at the function on line 46 to 48 and found out that there is nothing in the function.


    Code:
    <DllImport("user32.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Auto)> _
        Public Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As HookProc, ByVal hInstance As IntPtr, ByVal threadId As Integer) As Integer
        End Function
    I am not familiar with DllImport. Did i miss out something? Do i need to add any reference or import any dll into this project? I believe i must have missed out some steps. Please advise. Thanks a lot in advance.

  14. #14
    New Member
    Join Date
    Nov 2010
    Posts
    2

    Re: [RESOLVED]Capturing mouse click event in vb.net(Thanks so much Mr.Polite)

    Juz saw this FAQ question in the link above. Done that and tested that. Works fine. Sorry for not going through the link before asking..

    Question
    The project cannot be run in Visual Studio .NET 2005 in debug mode because of an exception error caused by calling the SetWindowsHookEx. Why? Is it a problem of .NET 2.0?

    Answer
    The compiled release version works well so that cannot be a .NET 2.0 problem. To workaround, you just need to uncheck the check box in the project properties that says: "Enable Visual Studio hosting process". In the menu: Project -> Project Properties... -> Debug -> Enable the Visual Studio hosting process.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width