# Visual Basic 2008 Mouse X,Y Detector



## Darow (Jan 17, 2009)

Alright so I'm making a x,y detector that detects the xy OUTSIDE the program,But for some reason it wont detect the x,y(lame).
(Also if anyone can help,im looking to make it so if the mouse is at a certain x,y then it should push the right arrow key)

Heres the source files
UserActivityHook.vb






```
Imports System.Runtime.InteropServices




Public Class UserActivityHook

    Public Delegate Function HookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer



    Public KeyboardHookProcedure As HookProc

    Public KeyDown As KeyEventHandler

    Public KeyPress As KeyPressEventHandler

    Public KeyUp As KeyEventHandler



    Public OnMouseActivity As MouseEventHandler





    Private Shared hKeyboardHook As Integer

    Private Shared hMouseHook As Integer

    Public MouseHookProcedure As HookProc

    Public Const WH_KEYBOARD_LL As Integer = 13

    Public Const WH_MOUSE_LL As Integer = 14

    Private Const WM_KEYDOWN As Integer = 256

    Private Const WM_KEYUP As Integer = 257

    Private Const WM_LBUTTONDBLCLK As Integer = 515

    Private Const WM_LBUTTONDOWN As Integer = 513

    Private Const WM_LBUTTONUP As Integer = 514

    Private Const WM_MBUTTONDBLCLK As Integer = 521

    Private Const WM_MBUTTONDOWN As Integer = 519

    Private Const WM_MBUTTONUP As Integer = 520

    Private Const WM_MOUSEMOVE As Integer = 512

    Private Const WM_RBUTTONDBLCLK As Integer = 518

    Private Const WM_RBUTTONDOWN As Integer = 516

    Private Const WM_RBUTTONUP As Integer = 517

    Private Const WM_SYSKEYDOWN As Integer = 260

    Private Const WM_SYSKEYUP As Integer = 261



#Region "dll imports"

    ' Methods

    <DllImport("user32.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Auto)> _
Public Shared Function CallNextHookEx(ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer

    End Function



    <DllImport("user32")> _
Public Shared Function GetKeyboardState(ByVal pbKeyState As Byte()) As Integer

    End Function






    <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



    <DllImport("user32")> _
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

    End Function



    <DllImport("user32.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Auto)> _
Public Shared Function UnhookWindowsHookEx(ByVal idHook As Integer) As Boolean

    End Function

#End Region



#Region "Structures and whatnot"

    <StructLayout(LayoutKind.Sequential)> _
Public Class KeyboardHookStruct

        ' Fields

        Public dwExtraInfo As Integer

        Public flags As Integer

        Public scanCode As Integer

        Public time As Integer

        Public vkCode As Integer

    End Class



    <StructLayout(LayoutKind.Sequential)> _
Public Class MouseHookStruct

        ' Fields

        Public dwExtraInfo As Integer

        Public hwnd As Integer

        Public pt As POINT

        Public wHitTestCode As Integer

    End Class



    <StructLayout(LayoutKind.Sequential)> _
Public Class POINT

        ' Fields

        Public x As Integer

        Public y As Integer

    End Class

#End Region









    Public Sub New()

        Start()
92:
    End Sub



    Protected Overrides Sub Finalize()

        Try

            Me.Stop()

        Finally

            MyBase.Finalize()

        End Try

    End Sub











    Public Sub Start()

        If (UserActivityHook.hMouseHook = 0) Then

            Me.MouseHookProcedure = New HookProc(AddressOf Me.MouseHookProc)

            UserActivityHook.hMouseHook = UserActivityHook.SetWindowsHookEx(14, Me.MouseHookProcedure, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)), 0)

            If (UserActivityHook.hMouseHook = 0) Then

                Me.Stop()

            End If

        End If

        If (UserActivityHook.hKeyboardHook = 0) Then

            Me.KeyboardHookProcedure = New HookProc(AddressOf Me.KeyboardHookProc)

            UserActivityHook.hKeyboardHook = UserActivityHook.SetWindowsHookEx(13, Me.KeyboardHookProcedure, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)), 0)

            If (UserActivityHook.hKeyboardHook = 0) Then

                Me.Stop()

            End If

        End If

    End Sub



    Public Sub [Stop]()

        Dim flag1 As Boolean = True

        Dim flag2 As Boolean = True

        If (UserActivityHook.hMouseHook <> 0) Then

            flag1 = UserActivityHook.UnhookWindowsHookEx(UserActivityHook.hMouseHook)

            UserActivityHook.hMouseHook = 0

        End If

        If (UserActivityHook.hKeyboardHook <> 0) Then

            flag2 = UserActivityHook.UnhookWindowsHookEx(UserActivityHook.hKeyboardHook)

            UserActivityHook.hKeyboardHook = 0

        End If

        If (Not flag1 OrElse Not flag2) Then

            Throw New Exception("UnhookWindowsHookEx failed.")

        End If

    End Sub





    Private Function MouseHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer

        If ((nCode >= 0) AndAlso (Not Me.OnMouseActivity Is Nothing)) Then

            Dim buttons1 As MouseButtons = MouseButtons.None

            Dim num3 As Integer = wParam
            If (num3 <> 513) Then

                If (num3 = 516) Then

                    buttons1 = MouseButtons.Right

                End If

            Else

                buttons1 = MouseButtons.Left

            End If

            Dim num1 As Integer = 0

            If (buttons1 <> MouseButtons.None) Then

                If ((wParam = 515) OrElse (wParam = 518)) Then

                    num1 = 2

                Else

                    num1 = 1

                End If

            End If

            Dim struct1 As MouseHookStruct = CType(Marshal.PtrToStructure(lParam, GetType(MouseHookStruct)), MouseHookStruct)

            Dim args1 As New MouseEventArgs(buttons1, num1, struct1.pt.x, struct1.pt.y, 0)

            Me.OnMouseActivity.Invoke(Me, args1)

        End If

        Return UserActivityHook.CallNextHookEx(UserActivityHook.hMouseHook, nCode, wParam, lParam)

    End Function





    Private Function KeyboardHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer

        If ((nCode >= 0) AndAlso (((Not Me.KeyDown Is Nothing) OrElse (Not Me.KeyUp Is Nothing)) OrElse (Not Me.KeyPress Is Nothing))) Then

            Dim struct1 As KeyboardHookStruct = CType(Marshal.PtrToStructure(lParam, GetType(KeyboardHookStruct)), KeyboardHookStruct)

            If ((Not Me.KeyDown Is Nothing) AndAlso ((wParam = 256) OrElse (wParam = 260))) Then

                Dim args1 As New KeyEventArgs(CType(struct1.vkCode, Keys))

                Me.KeyDown.Invoke(Me, args1)

            End If

            If ((Not Me.KeyPress Is Nothing) AndAlso (wParam = 256)) Then

                Dim buffer1 As Byte() = New Byte(256 - 1) {}

                UserActivityHook.GetKeyboardState(buffer1)

                Dim buffer2 As Byte() = New Byte(2 - 1) {}

                If (UserActivityHook.ToAscii(struct1.vkCode, struct1.scanCode, buffer1, buffer2, struct1.flags) = 1) Then

                    Dim args2 As New KeyPressEventArgs(Convert.ToChar(buffer2(0)))

                    Me.KeyPress.Invoke(Me, args2)

                End If

            End If

            If ((Not Me.KeyUp Is Nothing) AndAlso ((wParam = 257) OrElse (wParam = 261))) Then

                Dim args3 As New KeyEventArgs(CType(struct1.vkCode, Keys))

                Me.KeyUp.Invoke(Me, args3)

            End If

        End If

        Return UserActivityHook.CallNextHookEx(UserActivityHook.hKeyboardHook, nCode, wParam, lParam)

    End Function



End Class
```
Form1.vb

```
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 textBox As System.Windows.Forms.TextBox

    Friend WithEvents labelMousePosition As System.Windows.Forms.Label

    Friend WithEvents buttonStop As System.Windows.Forms.Button

    Friend WithEvents buttonStart As System.Windows.Forms.Button

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.textBox = New System.Windows.Forms.TextBox
        Me.labelMousePosition = New System.Windows.Forms.Label
        Me.buttonStop = New System.Windows.Forms.Button
        Me.buttonStart = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'textBox
        '
        Me.textBox.Font = New System.Drawing.Font("Courier New", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World)
        Me.textBox.Location = New System.Drawing.Point(8, 126)
        Me.textBox.Multiline = True
        Me.textBox.Name = "textBox"
        Me.textBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.textBox.Size = New System.Drawing.Size(280, 240)
        Me.textBox.TabIndex = 7
        '
        'labelMousePosition
        '
        Me.labelMousePosition.Location = New System.Drawing.Point(40, 94)
        Me.labelMousePosition.Name = "labelMousePosition"
        Me.labelMousePosition.Size = New System.Drawing.Size(184, 23)
        Me.labelMousePosition.TabIndex = 6
        Me.labelMousePosition.Text = "labelMousePosition"
        '
        'buttonStop
        '
        Me.buttonStop.Location = New System.Drawing.Point(144, 46)
        Me.buttonStop.Name = "buttonStop"
        Me.buttonStop.Size = New System.Drawing.Size(75, 23)
        Me.buttonStop.TabIndex = 5
        Me.buttonStop.Text = "Stop"
        '
        'buttonStart
        '
        Me.buttonStart.Location = New System.Drawing.Point(40, 46)
        Me.buttonStart.Name = "buttonStart"
        Me.buttonStart.Size = New System.Drawing.Size(75, 23)
        Me.buttonStart.TabIndex = 4
        Me.buttonStart.Text = "Start"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(296, 413)
        Me.Controls.Add(Me.textBox)
        Me.Controls.Add(Me.labelMousePosition)
        Me.Controls.Add(Me.buttonStop)
        Me.Controls.Add(Me.buttonStart)
        Me.Name = "Form1"
        Me.Text = "XY"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub



#End Region



    Private actHook As UserActivityHook

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.actHook = New UserActivityHook

        Me.actHook.OnMouseActivity = New MouseEventHandler(AddressOf MouseMoved)

        Me.actHook.KeyDown = New KeyEventHandler(AddressOf MyKeyDown)

        Me.actHook.KeyPress = New KeyPressEventHandler(AddressOf MyKeyPress)

        Me.actHook.KeyUp = New KeyEventHandler(AddressOf MyKeyUp)

    End Sub



    Private Sub buttonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonStart.Click

        actHook.Start()

    End Sub



    Private Sub buttonStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonStop.Click

        actHook.Stop()

    End Sub



    Public Sub MouseMoved(ByVal sender As Object, ByVal e As MouseEventArgs)

        Me.labelMousePosition.Text = String.Format("x={0}  y={1}", e.X, e.Y)

        If (e.Clicks > 0) Then

            Me.LogWrite(("MouseButton " & ChrW(9) & "- " & e.Button.ToString))

        End If

    End Sub





    Public Sub MyKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)

        Me.LogWrite(("KeyDown " & ChrW(9) & "- " & e.KeyData.ToString))

    End Sub





    Public Sub MyKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)

        Me.LogWrite(("KeyPress " & ChrW(9) & "- " & e.KeyChar))

    End Sub




    Public Sub MyKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)

        Me.LogWrite(("KeyUp " & ChrW(9) & ChrW(9) & "- " & e.KeyData.ToString))

    End Sub









    Private Sub LogWrite(ByVal txt As String)

        Me.textBox.AppendText((txt & Environment.NewLine))

        Me.textBox.SelectionStart = Me.textBox.Text.Length

    End Sub

End Class
```


----------

