Joystick auslesen

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von EIKI.

    Joystick auslesen

    Ich weiß wir hatten das Thema schon oft ...
    Leider bin ich trotzdem noch zu keiner Lösung gekommen.
    Mein bisheriger Code sieht so aus:

    Quellcode

    1. Imports System
    2. Imports System.Windows.Forms
    3. Imports Microsoft.DirectX.DirectInput
    4. Imports Microsoft.DirectX
    5. Public Class Joystick
    6. Private applicationDevice As Device = Nothing
    7. Public Shared state As New JoystickState()
    8. Public Function InitDirectInput() As Boolean 'wir erstellen eine Funktion die die Verbindung zum Joystick herstellt
    9. Dim instance As DeviceInstance
    10. For Each instance In Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly)
    11. applicationDevice = New Device(instance.InstanceGuid)
    12. Exit For
    13. Next instance
    14. applicationDevice.SetDataFormat(DeviceDataFormat.Joystick)
    15. applicationDevice.SetCooperativeLevel(Me, CooperativeLevelFlags.Exclusive Or CooperativeLevelFlags.Foreground)
    16. Dim d As DeviceObjectInstance
    17. For Each d In applicationDevice.Objects
    18. If 0 <> (d.ObjectId And CInt(DeviceObjectTypeFlags.Axis)) Then
    19. ' Set the range for the axis.
    20. applicationDevice.Properties.SetRange(ParameterHow.ById, d.ObjectId, New InputRange(-100000, +100000)) 'hier kann man die Auflösung des Joysticks beliebig einstellen
    21. End If
    22. Next d
    23. Return True
    24. End Function 'InitDirectInput
    25. Public Sub GetData()
    26. If Nothing Is applicationDevice Then
    27. Return
    28. End If
    29. Try
    30. applicationDevice.Poll()
    31. Catch inputex As InputException
    32. If TypeOf inputex Is NotAcquiredException Or TypeOf inputex Is InputLostException Then
    33. Try
    34. applicationDevice.Acquire()
    35. Catch
    36. Return
    37. End Try
    38. End If
    39. End Try
    40. Try
    41. state = applicationDevice.CurrentJoystickState
    42. Catch
    43. Return
    44. End Try
    45. End Sub 'GetData
    46. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    47. GetData() 'Joystick Position erfassen
    48. Label1.Text = "Joystick: X = " + state.X.ToString() + " Y = " + state.Y.ToString() ' Anzeigen wo der Joystick grad ist
    49. Label3.Text = "Joystick: Z achse= " & state.Z.ToString 'Anzeige des Geschwindigkeitsreglers
    50. Label4.Text = "Joystick: Z rotation= " & state.Rz.ToString 'Anzeige der rechts links Rotation
    51. End Sub
    52. Private Sub Joystick_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Button1.Click
    53. Try
    54. InitDirectInput()
    55. Catch
    56. MsgBox("Es konnte kein Joystick gefunden werden" & vbCrLf & "Bitte überprüfen sie die Verbindung")
    57. End Try
    58. End Sub
    59. End Class


    Ich weiß der Code ist lang, aber bitte schaut ihn euch doch nochmal an und helft mir weiter, ich verzweifel sonst noch irgendwann :(

    Danke für eure Hilfe
    Sorry das ich in dieses alte Thema reinschreibe, aber als ich ein neues erstellen wollte bin ich hier drüber gestolpert.
    Ich will auch meinen Joystick(Competition Pro USB Joystick) mit einem VB Programm auslesen hab aber leider noch nichts für mich verwertbares dazu gefunden. Vielleicht kennt ja irgendwer mittlerweile einen Funktionierenden Code.

    MFG EIKI
    Hi
    versuche dich mal an einem DirectX-Wrapper, der DirectInput mitbringt (z.B. SharpDX). Funktionierenden Code würde ich nicht empfehlen, da sollst du schon etwas dabei lernen. Man kann auch eine notfication registrieren, die ein WaitHandle signalisiert, sobald eine Zustandsänderung eintritt, btw.

    Gruß
    ~blaze~