battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.

battle programmers allianceLog in

the LivinGrimoire Artificial General Intelligence software design pattern forum

descriptionvb.net get joystick input Emptyvb.net get joystick input

more_horiz
the following code gets joystick arrows and buttons pressed at realtime

by jo0ls :

Create a new windows forms application.
Use Project -> Add class to add a new class called "Joystick.vb"
In the Joystick.vb code file, replace the existing code with the following:


Code:

Imports System.ComponentModel
Imports System.Runtime.InteropServices

Public Class Joystick
    Inherits NativeWindow

    Private parent As Form
    Private Const MM_JOY1MOVE As Integer = &H3A0

    ' Public Event Move(ByVal joystickPosition As Point)
    Public btnValue As String
    Public Event Up()
    Public Event Down()
    Public Event Left()
    Public Event Right()

    <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

        ' This is a "Stub" function - it has no code in its body.
        ' There is a similarly named function inside a dll that comes with windows called
        ' winmm.dll.
        ' The .Net framework will route calls to this function, through to the dll file.
        <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 = MM_JOY1MOVE Then
            ' Joystick co-ords.
            ' (0,0) (32768,0) (65535, 0)
            '
            '
            '
            ' (0, 32768) (32768, 32768) (65535, 32768)
            '
            '
            '
            '
            ' (0, 65535) (32768, 65535) (65535, 65535)
            '
            Dim p As JoyPosition
            p.Raw = m.LParam
            ' RaiseEvent Move(New Point(p.XPos, p.YPos))
            If p.XPos > 16384 AndAlso p.XPos < 49152 Then
                ' X is near the centre line.
                If p.YPos < 6000 Then
                    ' Y is near the top.
                    RaiseEvent Up()
                ElseIf p.YPos > 59536 Then
                    ' Y is near the bottom.
                    RaiseEvent Down()
                End If
            Else
                If p.YPos > 16384 AndAlso p.YPos < 49152 Then
                    ' Y is near the centre line
                    If p.XPos < 6000 Then
                        ' X is near the left.
                        RaiseEvent Left()
                    ElseIf p.XPos > 59536 Then
                        ' X is near the right
                        RaiseEvent Right()
                    End If
                End If
            End If
        End If
        If btnValue <> m.WParam.ToString Then
            btnValue = m.WParam.ToString
        End If
        MyBase.WndProc(m)
    End Sub

End Class


Now you can ignore the Joystick.vb code - you aren't supposed to be able to understand it on day 3 of learning VB.Net. Maybe on day 303...
Now you can create an instance of the Joystick class in your Form1 class, and handle the events:
add a timer and a label from the toolbox (in form1.vb[design] window)

Code:

Public Class Form1

    ' This declares what Type the variable joystick1 will be for. The Type is Joystick.
    ' WithEvents allows you to easily add events using the IDE.
    Private WithEvents joystick1 As Joystick

    ' This is an event that belongs to the Form. It is raised when the form loads.
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Here we create the Joystick object. You must pass Me - which refers to this Form,
        ' and 0 - which is the Joystick id.
        joystick1 = New Joystick(Me, 0)
    End Sub

    ' And now we have the four events that belong to joystick1.

    Private Sub joystick1_Down() Handles joystick1.Down
        ' TODO: Replace this so that it plays a sound instead.
        Me.Text = "Down"
    End Sub

    Private Sub joystick1_Left() Handles joystick1.Left
        ' TODO: Replace this so that it plays a sound instead.
        Me.Text = "Left"
    End Sub

    Private Sub joystick1_Right() Handles joystick1.Right
        ' TODO: Replace this so that it plays a sound instead.
        Me.Text = "Right"
    End Sub

    Private Sub joystick1_Up() Handles joystick1.Up
        ' TODO: Replace this so that it plays a sound instead.
        Me.Text = "Up"
    End Sub
    ' Private Sub joystick1_buttonPressed() Handles joystick1.buttonPressed
    ' TODO: Replace this so that it plays a sound instead.
    '    Me.Text = joystick1.b1
    'End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = joystick1.btnValue
    End Sub
End Class


this code can also be found for free in the battle programming book series in bp 2nd part.txt

descriptionvb.net get joystick input Emptydclarkchem

more_horiz
@dclarkchem please post in the thread like this post.
how to get the buttonpressed to work?
it does work, maybe you need to try a different joystick.
maybe I didn't understand your question.

descriptionvb.net get joystick input Emptyvb.net get joystick input

more_horiz
Perhaps this code doesn't work in Visual Studio 11.

I've tried several USB joysticks and I can't get it to recognize a button being pressed.

thanks for any help.

dclarkchem at aol.com

descriptionvb.net get joystick input Emptypost 1

more_horiz
assuming you are using the code in this thread and not the older one it works in vb.net 2010 express edition, maybe the vs 2011 beta isn't complete for joysticks.

descriptionvb.net get joystick input EmptyVB net 11

more_horiz
You could very well be correct, I just thought that 11 would be very robust. I'll have another look at it next week.

I'm considering forgetting joysticks and using smart phones to ring into the program via WI-FI. That should be fun to code! (not!!)

descriptionvb.net get joystick input Emptygot it to read

more_horiz
kurosen wrote:
assuming you are using the code in this thread and not the older one it works in vb.net 2010 express edition, maybe the vs 2011 beta isn't complete for joysticks.



The "up", "down", "left" and "right" in VB.net 11, beta, but having troubles with getting the buttons to be recognized.

Any help would be appreciated. Thanks!

descriptionvb.net get joystick input Emptyproblema

more_horiz
I retried the code on my latest vb.net 2010 express, and it doesn't even get the arroy keys
and yes the timer was enables, and I get some error, seems to me that the support for joystick was stoped I'll just use a wireless keyboard, totally not cool. it might get fixed by installing prev framworks.
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply