Results 1 to 2 of 2

Thread: Help Joystick Hook

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2016
    Posts
    7

    Help Joystick Hook

    I'm using this code for a real time Joystick events, works very well but only check Directional buttons like up,down,right,left and the 4 buttons X,O,Y,L
    I need to read the extra buttons like L1,L2,L3,L4, Start etc, but don't recognize


    Class
    Code:
    Imports System.ComponentModel
    Imports System.Runtime.InteropServices
    
    Public Class Joystick
        Inherits NativeWindow
    
        Private parent As Form
        Private Const MM_JOY1BUTTONDOWN As Long = &H3B5
        Private Const MM_JOY1BUTTONUP As Long = &H3B7
        Private Const MM_JOY1MOVE As Long = &H3A0
        Private Const MM_JOY1ZMOVE As Long = &H3A2
        'Public Event Move(ByVal joystickPosition As Point)
        Public btnValue As String
        Public Event Movement()
        Public Event BtnDown()
        Public Event BtnUp()
    
    
        <StructLayout(LayoutKind.Explicit)>
        Private Structure JoyPosition
            <FieldOffset(0)>
            Public Raw As IntPtr
            <FieldOffset(0)>
            Public XPos As UShort
            <FieldOffset(2)>
            Public YPos As UShort
        End Structure
    
        Private Class NativeMethods
    
            Private Sub New()
            End Sub
    
            <DllImport("winmm", CallingConvention:=CallingConvention.Winapi, EntryPoint:="joySetCapture", SetLastError:=True)>
            Public Shared Function JoySetCapture(ByVal hwnd As IntPtr, ByVal uJoyID As Integer, ByVal uPeriod As Integer, <MarshalAs(UnmanagedType.Bool)> ByVal changed As Boolean) As Integer
            End Function
    
        End Class
    
        Public Sub New(ByVal parent As Form, ByVal joyId As Integer)
            AddHandler parent.HandleCreated, AddressOf Me.OnHandleCreated
            AddHandler parent.HandleDestroyed, AddressOf Me.OnHandleDestroyed
            AssignHandle(parent.Handle)
            Me.parent = parent
            Dim result As Integer = NativeMethods.JoySetCapture(Me.Handle, joyId, 100, True)
        End Sub
    
        Private Sub OnHandleCreated(ByVal sender As Object, ByVal e As EventArgs)
            AssignHandle(DirectCast(sender, Form).Handle)
        End Sub
    
        Private Sub OnHandleDestroyed(ByVal sender As Object, ByVal e As EventArgs)
            ReleaseHandle()
        End Sub
    
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            
            If m.Msg = JOY1MOVE Then
                RaiseEvent Movement()
            End If
            If m.Msg = MM_JOY1BUTTONDOWN Then
                RaiseEvent BtnDown()
            End If
    
            MyBase.WndProc(m)
        End Sub
    
    End Class
    In main form put
    Code:
        Private WithEvents joystick1 As Joystick
    
        Private Sub joystick1_BtnDown() Handles joystick1.BtnDown
    
        End Sub
        Private Sub joystick1_Movement() Handles joystick1.Movement
    
        End Sub
    In form load put
    Code:
    joystick1 = New Joystick(Me, 0)

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,945

Tags for this Thread

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