TizzyT

XInput and Keys -TizzyT

Jul 12th, 2015
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.87 KB | None | 0 0
  1. Imports System.Windows.Input
  2. Imports System.Runtime.InteropServices
  3. Public Class Inputs
  4.     Private Declare Function XInputGetBatteryInformation Lib "xinput1_3.dll" (ByVal uJoyID As Integer, _
  5.                                                                               ByVal uJoyID2 As UInteger, _
  6.                                                                               ByRef GameState As XINPUT_BATTERY_INFORMATION) As Int16
  7.     Private Declare Function XInputGetState Lib "xinput1_3.dll" (ByVal uJoyID As Integer, ByRef GameState As XINPUT_STATE) As Boolean
  8.     Private Declare Function XInputSetState Lib "xinput1_3.dll" (ByVal uJoyID As Integer, ByRef GameState As XINPUT_VIBRATION) As Int16
  9.     Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Short) As Integer
  10.     Private Shared _MessageCounter As ULong = 0
  11.     Private Ctrls As New InputCollection({})
  12.     Public Structure XINPUT_BATTERY_INFORMATION
  13.         Public BatteryType, BatteryLevel As Byte
  14.     End Structure
  15.     Public Structure XINPUT_GAMEPAD
  16.         Public wButtons As UInt16
  17.         Public bLeftTrigger , bRightTrigger As Char
  18.         Public sThumbLX, sThumbLY , sThumbRX , sThumbRY As Int16
  19.         Public Function Equal(ByVal CompareTo As XINPUT_GAMEPAD) As Boolean
  20.             If wButtons <> CompareTo.wButtons Then Return False
  21.             If bLeftTrigger <> CompareTo.bLeftTrigger Then Return False
  22.             If bRightTrigger <> CompareTo.bRightTrigger Then Return False
  23.             If sThumbLX <> CompareTo.sThumbLX Then Return False
  24.             If sThumbLY <> CompareTo.sThumbLY Then Return False
  25.             If sThumbRX <> CompareTo.sThumbRX Then Return False
  26.             If sThumbRY <> CompareTo.sThumbRY Then Return False
  27.             Return True
  28.         End Function
  29.     End Structure
  30.     Public Structure XINPUT_STATE
  31.         Public dwPacketNumber As Integer
  32.         Public Gamepad As XINPUT_GAMEPAD
  33.     End Structure
  34.     Public Structure XINPUT_VIBRATION
  35.         Public wLeftMotorSpeed ,wRightMotorSpeed As UShort
  36.         Sub New(ByVal LMotor As Double, ByVal RMotor As Double)
  37.             If LMotor <= 0 Then
  38.                 wLeftMotorSpeed = 0
  39.             Else
  40.                 If LMotor > 100 Then LMotor = 100
  41.                 wLeftMotorSpeed = ((LMotor * 61170) / 100) + 4365
  42.             End If
  43.             If RMotor <= 0 Then
  44.                 wRightMotorSpeed = 0
  45.             Else
  46.                 If RMotor > 100 Then RMotor = 100
  47.                 wRightMotorSpeed = ((RMotor * 65017) / 100) + 518
  48.             End If
  49.         End Sub
  50.     End Structure
  51.     Private Shared KeysList As New List(Of Keys)
  52.     Public Sub AddKeyboardKey(ByVal Key As Keys)
  53.         If Not KeysList.Contains(Key) Then KeysList.Add(Key)
  54.     End Sub
  55.     Public Sub RemoveKeyboardkey(ByVal key As Keys)
  56.         If KeysList.Contains(key) Then KeysList.Remove(key)
  57.     End Sub
  58.     Public Sub ClearKeyboardKeys()
  59.         KeysList.Clear()
  60.     End Sub
  61.     Public Structure InputCollection
  62.         Public Controller() As XINPUT_STATE
  63.         Public RequestID As ULong
  64.         Public Keys As Dictionary(Of Keys, Boolean)
  65.         Public Sub New(ByVal Controllers() As XINPUT_STATE)
  66.             Controller = Controllers
  67.             Keys = New Dictionary(Of Keys, Boolean)
  68.             For i = 0 To KeysList.Count - 1
  69.                 If GetKeyState(KeysList(i)) < 0 Then Keys.Add(KeysList(i), True) Else Keys.Add(KeysList(i), False)
  70.             Next
  71.             RequestID = _MessageCounter
  72.             _MessageCounter += 1
  73.         End Sub
  74.     End Structure
  75.     Public Sub VibrateController(ByVal LMotorPercent As Double,
  76.                                  ByVal RMotorPercent As Double, _
  77.                                  Optional ByVal JoyID As SByte = -1)
  78.         If JoyID < 0 OrElse JoyID > 3 Then
  79.             XInputSetState(0, New XINPUT_VIBRATION(LMotorPercent, RMotorPercent))
  80.             XInputSetState(1, New XINPUT_VIBRATION(LMotorPercent, RMotorPercent))
  81.             XInputSetState(2, New XINPUT_VIBRATION(LMotorPercent, RMotorPercent))
  82.             XInputSetState(3, New XINPUT_VIBRATION(LMotorPercent, RMotorPercent))
  83.         Else
  84.             XInputSetState(JoyID, New XINPUT_VIBRATION(LMotorPercent, RMotorPercent))
  85.         End If
  86.     End Sub
  87.     Public Sub StopVibratae(Optional ByVal JoyID As SByte = -1)
  88.         If JoyID < 0 OrElse JoyID > 3 Then
  89.             XInputSetState(0, New XINPUT_VIBRATION(0, 0))
  90.             XInputSetState(1, New XINPUT_VIBRATION(0, 0))
  91.             XInputSetState(2, New XINPUT_VIBRATION(0, 0))
  92.             XInputSetState(3, New XINPUT_VIBRATION(0, 0))
  93.         Else
  94.             XInputSetState(JoyID, New XINPUT_VIBRATION(0, 0))
  95.         End If
  96.     End Sub
  97.     Default Public ReadOnly Property Inputs(ByVal RefreshInputs As Boolean) As InputCollection
  98.         Get
  99.             If RefreshInputs Then
  100.                 Dim ControllerStates(3) As XINPUT_STATE
  101.                 XInputGetState(0, ControllerStates(0))
  102.                 XInputGetState(1, ControllerStates(1))
  103.                 XInputGetState(2, ControllerStates(2))
  104.                 XInputGetState(3, ControllerStates(3))
  105.                 Ctrls = New InputCollection(ControllerStates)
  106.             End If
  107.             Return Ctrls
  108.         End Get
  109.     End Property
  110.     Public Function GetXInputs(Optional ByVal JoyID As SByte = -1) As InputCollection
  111.         If JoyID < -1 OrElse JoyID > 3 Then
  112.             Dim ControllerStates(3) As XINPUT_STATE
  113.             XInputGetState(0, ControllerStates(0))
  114.             XInputGetState(1, ControllerStates(1))
  115.             XInputGetState(2, ControllerStates(2))
  116.             XInputGetState(3, ControllerStates(3))
  117.             Return New InputCollection(ControllerStates)
  118.         Else
  119.             Dim ControllerState(0) As XINPUT_STATE
  120.             XInputGetState(JoyID, ControllerState(0))
  121.             Return New InputCollection(ControllerState)
  122.         End If
  123.     End Function
  124. End Class
Advertisement
Add Comment
Please, Sign In to add comment