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:
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)
this code can also be found for free in the battle programming book series in bp 2nd part.txt
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