Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Windows.Input
- Imports System.Runtime.InteropServices
- Public Class Inputs
- Private Declare Function XInputGetBatteryInformation Lib "xinput1_3.dll" (ByVal uJoyID As Integer, _
- ByVal uJoyID2 As UInteger, _
- ByRef GameState As XINPUT_BATTERY_INFORMATION) As Int16
- Private Declare Function XInputGetState Lib "xinput1_3.dll" (ByVal uJoyID As Integer, ByRef GameState As XINPUT_STATE) As Boolean
- Private Declare Function XInputSetState Lib "xinput1_3.dll" (ByVal uJoyID As Integer, ByRef GameState As XINPUT_VIBRATION) As Int16
- Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Short) As Integer
- Private Shared _MessageCounter As ULong = 0
- Private Ctrls As New InputCollection({})
- Public Structure XINPUT_BATTERY_INFORMATION
- Public BatteryType, BatteryLevel As Byte
- End Structure
- Public Structure XINPUT_GAMEPAD
- Public wButtons As UInt16
- Public bLeftTrigger , bRightTrigger As Char
- Public sThumbLX, sThumbLY , sThumbRX , sThumbRY As Int16
- Public Function Equal(ByVal CompareTo As XINPUT_GAMEPAD) As Boolean
- If wButtons <> CompareTo.wButtons Then Return False
- If bLeftTrigger <> CompareTo.bLeftTrigger Then Return False
- If bRightTrigger <> CompareTo.bRightTrigger Then Return False
- If sThumbLX <> CompareTo.sThumbLX Then Return False
- If sThumbLY <> CompareTo.sThumbLY Then Return False
- If sThumbRX <> CompareTo.sThumbRX Then Return False
- If sThumbRY <> CompareTo.sThumbRY Then Return False
- Return True
- End Function
- End Structure
- Public Structure XINPUT_STATE
- Public dwPacketNumber As Integer
- Public Gamepad As XINPUT_GAMEPAD
- End Structure
- Public Structure XINPUT_VIBRATION
- Public wLeftMotorSpeed ,wRightMotorSpeed As UShort
- Sub New(ByVal LMotor As Double, ByVal RMotor As Double)
- If LMotor <= 0 Then
- wLeftMotorSpeed = 0
- Else
- If LMotor > 100 Then LMotor = 100
- wLeftMotorSpeed = ((LMotor * 61170) / 100) + 4365
- End If
- If RMotor <= 0 Then
- wRightMotorSpeed = 0
- Else
- If RMotor > 100 Then RMotor = 100
- wRightMotorSpeed = ((RMotor * 65017) / 100) + 518
- End If
- End Sub
- End Structure
- Private Shared KeysList As New List(Of Keys)
- Public Sub AddKeyboardKey(ByVal Key As Keys)
- If Not KeysList.Contains(Key) Then KeysList.Add(Key)
- End Sub
- Public Sub RemoveKeyboardkey(ByVal key As Keys)
- If KeysList.Contains(key) Then KeysList.Remove(key)
- End Sub
- Public Sub ClearKeyboardKeys()
- KeysList.Clear()
- End Sub
- Public Structure InputCollection
- Public Controller() As XINPUT_STATE
- Public RequestID As ULong
- Public Keys As Dictionary(Of Keys, Boolean)
- Public Sub New(ByVal Controllers() As XINPUT_STATE)
- Controller = Controllers
- Keys = New Dictionary(Of Keys, Boolean)
- For i = 0 To KeysList.Count - 1
- If GetKeyState(KeysList(i)) < 0 Then Keys.Add(KeysList(i), True) Else Keys.Add(KeysList(i), False)
- Next
- RequestID = _MessageCounter
- _MessageCounter += 1
- End Sub
- End Structure
- Public Sub VibrateController(ByVal LMotorPercent As Double,
- ByVal RMotorPercent As Double, _
- Optional ByVal JoyID As SByte = -1)
- If JoyID < 0 OrElse JoyID > 3 Then
- XInputSetState(0, New XINPUT_VIBRATION(LMotorPercent, RMotorPercent))
- XInputSetState(1, New XINPUT_VIBRATION(LMotorPercent, RMotorPercent))
- XInputSetState(2, New XINPUT_VIBRATION(LMotorPercent, RMotorPercent))
- XInputSetState(3, New XINPUT_VIBRATION(LMotorPercent, RMotorPercent))
- Else
- XInputSetState(JoyID, New XINPUT_VIBRATION(LMotorPercent, RMotorPercent))
- End If
- End Sub
- Public Sub StopVibratae(Optional ByVal JoyID As SByte = -1)
- If JoyID < 0 OrElse JoyID > 3 Then
- XInputSetState(0, New XINPUT_VIBRATION(0, 0))
- XInputSetState(1, New XINPUT_VIBRATION(0, 0))
- XInputSetState(2, New XINPUT_VIBRATION(0, 0))
- XInputSetState(3, New XINPUT_VIBRATION(0, 0))
- Else
- XInputSetState(JoyID, New XINPUT_VIBRATION(0, 0))
- End If
- End Sub
- Default Public ReadOnly Property Inputs(ByVal RefreshInputs As Boolean) As InputCollection
- Get
- If RefreshInputs Then
- Dim ControllerStates(3) As XINPUT_STATE
- XInputGetState(0, ControllerStates(0))
- XInputGetState(1, ControllerStates(1))
- XInputGetState(2, ControllerStates(2))
- XInputGetState(3, ControllerStates(3))
- Ctrls = New InputCollection(ControllerStates)
- End If
- Return Ctrls
- End Get
- End Property
- Public Function GetXInputs(Optional ByVal JoyID As SByte = -1) As InputCollection
- If JoyID < -1 OrElse JoyID > 3 Then
- Dim ControllerStates(3) As XINPUT_STATE
- XInputGetState(0, ControllerStates(0))
- XInputGetState(1, ControllerStates(1))
- XInputGetState(2, ControllerStates(2))
- XInputGetState(3, ControllerStates(3))
- Return New InputCollection(ControllerStates)
- Else
- Dim ControllerState(0) As XINPUT_STATE
- XInputGetState(JoyID, ControllerState(0))
- Return New InputCollection(ControllerState)
- End If
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment