Question

Setup Environment:

I'm using Visual Studio 2010 and writing code in vb.net (with .NET Framework 4).


My goal:

  1. Use Shell("C:\myprogram.exe", AppWinStyle.NormalFocus) to open a new application
  2. With new application open as the active window, recognize when user double clicks

If the active window is a vb.net form, it's simple to recognize double click event like so:

Private Sub Button2_DoubleClick(......) Handles Button2.DoubleClick

End Sub


This code works to recognize a SINGLE left click:

  • This is Form1.vb

    Public Class Form1
    
        Private WithEvents MouseDetector As MouseDetector
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            MouseDetector = New MouseDetector
    
        End Sub
    
        Private Sub MouseDetector_MouseLeftButtonClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MouseDetector.MouseLeftButtonClick
    
            'Left click occurred
            MsgBox("Left click")
    
        End Sub
    
    End Class
    


  • Here's the Enum in MouseDetector.vb

    Private Enum MouseMessages
        WM_LeftButtonDown = 513
        WM_LeftButtonUp = 514
        WM_LeftDblClick = 515
        WM_RightButtonDown = 516
        WM_RightButtonUp = 517
        WM_RightDblClick = 518
    End Enum
    


  • And here's the Function in MouseDetector.vb that recognizes the single click

    Private Function MouseHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
            If nCode < 0 Then
                Return CallNextHookEx(MouseHookID, nCode, wParam, lParam)
            End If
            Dim MouseData As MouseHookStruct = Marshal.PtrToStructure(lParam, GetType(MouseHookStruct))
            Select Case wParam
    
                Case MouseMessages.WM_LeftButtonUp
                    RaiseEvent MouseLeftButtonClick(Nothing, New MouseEventArgs(MouseButtons.Left, 1, MouseData.pt.x, MouseData.pt.y, 0))
    
            End Select
            Return CallNextHookEx(MouseHookID, nCode, wParam, lParam)
        End Function
    


ScreenShot

How is it possible to recognize a DOUBLE click?

Obviously there isn't a MouseButtons.LeftDoubleclick, but that's what I'm after.


Would someone be willing to give a suggestion on how to go about doing this? I'd really appreciate it.

Was it helpful?

Solution

If you look at the CodePlex GlobalMouseKeyHook project, they are handling the MouseDouble click events globally for both the Left and Right mouse buttons.

This library attaches to windows global hooks, tracks keyboard and mouse clicks and movement and raises common .NET events with KeyEventArgs and MouseEventArgs, so you can easily retrieve any information you need:
Mouse coordinates
Mouse buttons clicked
Mouse wheel scrolls
Key presses and releases
Special key states

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top