DirectInput Mouse probs

Darc

Well-known member
Joined
Apr 18, 2003
Messages
89
I dont know where Im supposed to post this but this IS the directx 9 forums so...

When i use the mouse, I set it all up and the clicking works fine but every time I try to get the X/Y positions, it returns 0. Any help?
 
Heres the clsDIMouse that I made:

Code:
Option Strict On
Option Explicit On 

Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectInput

Public Class clsDIMouse

    Private frmTarget As Form
    Private devMouse As Device

    Private lMouseX As Long
    Private lMouseY As Long

    Public Sub New(ByVal frm As Form)

        frmTarget = frm

        devMouse = New Device(SystemGuid.Mouse)
        devMouse.SetDataFormat(DeviceDataFormat.Mouse)
        devMouse.SetCooperativeLevel(frm, CooperativeLevelFlags.Foreground Or CooperativeLevelFlags.Exclusive)

        devMouse.Acquire()

    End Sub

    Private Function State() As MouseState
        Try
            Return devMouse.CurrentMouseState
        Catch
            Try
                devMouse.Acquire()
            Catch ex As InputException
                If TypeOf ex Is OtherApplicationHasPriorityException Or TypeOf ex Is NotAcquiredException Then check what type of input exception occured
                    Return Nothing
                End If
            End Try
        End Try
    End Function

    Public ReadOnly Property MouseX() As Long
        Get
            lMouseX = State.X
            Return lMouseX
        End Get
    End Property

    Public ReadOnly Property MouseY() As Long
        Get
            lMouseY += State.Y
            Return lMouseY
        End Get
    End Property

    Public ReadOnly Property LeftClick() As Boolean
        Get
            Dim temp As Byte() = State.GetMouseButtons()
            If temp(0) <> 0 Then
                Return True
            Else
                Return False
            End If
        End Get
    End Property

    Public ReadOnly Property RightClick() As Boolean
        Get
            Dim temp As Byte() = State.GetMouseButtons()
            If temp(1) <> 0 Then
                Return True
            Else
                Return False
            End If
        End Get
    End Property

    Public Function free() As Boolean
        Try
            devMouse.Unacquire()
            devMouse.Dispose()
            devMouse = Nothing
            Return True
        Catch
            Return False
        End Try
    End Function
End Class

And heres the funtion that sends an object to the mouse (Im not very good at this kind of math so I copied this from somewhere... I dont know if its the write equation :S)

Code:
    Public Function GoToMouse(ByVal sprPosX As Double, ByVal sprPosY As Double, ByVal sprPosSpeed As Double, ByVal mouseX As Double, ByVal mouseY As Double, ByRef MoveX As Double, ByRef MoveY As Double) As Boolean
        Try
            Dim xDiff As Double = CDbl(mouseX - sprPosX)
            Dim ydiff As Double = CDbl(mouseY - sprPosY)
            Dim Length As Double = xDiff * xDiff + ydiff * ydiff

            Length = Sqrt(Length)
            MoveX = (sprPosX - mouseX) / Length * sprPosSpeed
            MoveY = (sprPosY - mouseY) / Length * sprPosSpeed

        Catch
        End Try
    End Function
 
There is no Return in the GoToMouse Function so you should change it into a Sub since its always False anyway.

When you say "Get X/Y" do you mean clsDIMouse.MouseX and clsDIMouse.MouseY or GoToMouse

GoToMouse returns MoveX and MoveY. I think that DirectInput only returns the distance moved (As in move the mouse up then State will be 100[Y] then reset to 0 when the mouse stops) so you may need to track it manually if it still works like this. The documentation is most unhelpful so I cant tell you though.
 
Back
Top