Skip to content

Commit fe70c9f

Browse files
committed
Implement keyboard and controller recenter
1 parent d2af3ca commit fe70c9f

7 files changed

Lines changed: 469 additions & 108 deletions

File tree

PSMSVirtualDeviceManager/Classes/ClassControllerHook.vb

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,62 +60,85 @@ Public Class ClassControllerHook
6060
Private g_iPlayerIndex As ENUM_PLAYER_INDEX
6161
Private g_mCurrentState As New ClassWin32.XInputState
6262
Private g_mPreviousState As New ClassWin32.XInputState
63+
Private g_mThreadLock As New Object
6364

6465
Public Sub New(playerIndex As ENUM_PLAYER_INDEX)
6566
g_iPlayerIndex = playerIndex
6667
End Sub
6768

6869
Public Sub Update()
69-
g_mPreviousState = g_mCurrentState
70-
ClassWin32.XInputGetState(g_iPlayerIndex, g_mCurrentState)
70+
SyncLock g_mThreadLock
71+
g_mPreviousState = g_mCurrentState
72+
ClassWin32.XInputGetState(g_iPlayerIndex, g_mCurrentState)
73+
End SyncLock
7174
End Sub
7275

7376
Public Function AnyButtonDown() As Boolean
74-
Return (g_mCurrentState.Gamepad.Buttons <> 0)
77+
SyncLock g_mThreadLock
78+
Return (g_mCurrentState.Gamepad.Buttons <> 0)
79+
End SyncLock
7580
End Function
7681

7782
Public Function GetButtonDown() As ClassWin32.XInputButtons
78-
Return g_mCurrentState.Gamepad.Buttons
83+
SyncLock g_mThreadLock
84+
Return g_mCurrentState.Gamepad.Buttons
85+
End SyncLock
7986
End Function
8087

8188
Public Function IsButtonDown(iButton As ClassWin32.XInputButtons) As Boolean
82-
Return (g_mCurrentState.Gamepad.Buttons And iButton) = iButton
89+
SyncLock g_mThreadLock
90+
Return (g_mCurrentState.Gamepad.Buttons And iButton) = iButton
91+
End SyncLock
8392
End Function
8493

8594
Public Function IsButtonPressed(iButton As ClassWin32.XInputButtons) As Boolean
86-
Return (g_mCurrentState.Gamepad.Buttons And iButton) = iButton AndAlso
95+
SyncLock g_mThreadLock
96+
Return (g_mCurrentState.Gamepad.Buttons And iButton) = iButton AndAlso
8797
(g_mPreviousState.Gamepad.Buttons And iButton) <> iButton
98+
End SyncLock
8899
End Function
89100

90101
Public Function IsButtonReleased(iButton As ClassWin32.XInputButtons) As Boolean
91-
Return (g_mCurrentState.Gamepad.Buttons And iButton) <> iButton AndAlso
102+
SyncLock g_mThreadLock
103+
Return (g_mCurrentState.Gamepad.Buttons And iButton) <> iButton AndAlso
92104
(g_mPreviousState.Gamepad.Buttons And iButton) = iButton
105+
End SyncLock
93106
End Function
94107

95108
Public Function GetLeftTrigger() As Single
96-
Return g_mCurrentState.Gamepad.LeftTrigger / 255.0F
109+
SyncLock g_mThreadLock
110+
Return g_mCurrentState.Gamepad.LeftTrigger / 255.0F
111+
End SyncLock
97112
End Function
98113

99114
Public Function GetRightTrigger() As Single
100-
Return g_mCurrentState.Gamepad.RightTrigger / 255.0F
115+
SyncLock g_mThreadLock
116+
Return g_mCurrentState.Gamepad.RightTrigger / 255.0F
117+
End SyncLock
101118
End Function
102119

103120
Public Function GetLeftThumbstick() As Vector2
104-
Dim x As Single = g_mCurrentState.Gamepad.ThumbLX / 32768.0F
105-
Dim y As Single = g_mCurrentState.Gamepad.ThumbLY / 32768.0F
106-
Return New Vector2(If(Math.Abs(x) < 0.1F, 0.0F, x), If(Math.Abs(y) < 0.1F, 0.0F, y))
121+
SyncLock g_mThreadLock
122+
Dim x As Single = g_mCurrentState.Gamepad.ThumbLX / 32768.0F
123+
Dim y As Single = g_mCurrentState.Gamepad.ThumbLY / 32768.0F
124+
Return New Vector2(If(Math.Abs(x) < 0.1F, 0.0F, x), If(Math.Abs(y) < 0.1F, 0.0F, y))
125+
End SyncLock
107126
End Function
108127

109128
Public Function GetRightThumbstick() As Vector2
110-
Dim x As Single = g_mCurrentState.Gamepad.ThumbRX / 32768.0F
111-
Dim y As Single = g_mCurrentState.Gamepad.ThumbRY / 32768.0F
112-
Return New Vector2(If(Math.Abs(x) < 0.1F, 0.0F, x), If(Math.Abs(y) < 0.1F, 0.0F, y))
129+
SyncLock g_mThreadLock
130+
Dim x As Single = g_mCurrentState.Gamepad.ThumbRX / 32768.0F
131+
Dim y As Single = g_mCurrentState.Gamepad.ThumbRY / 32768.0F
132+
Return New Vector2(If(Math.Abs(x) < 0.1F, 0.0F, x), If(Math.Abs(y) < 0.1F, 0.0F, y))
133+
End SyncLock
113134
End Function
114135

115136
Public Function IsConnected() As Boolean
116137
Try
117-
Dim state As ClassWin32.XInputState
118-
Return ClassWin32.XInputGetState(g_iPlayerIndex, state) = 0
138+
SyncLock g_mThreadLock
139+
Dim state As ClassWin32.XInputState
140+
Return ClassWin32.XInputGetState(g_iPlayerIndex, state) = 0
141+
End SyncLock
119142
Catch ex As Exception
120143
Return False
121144
End Try

PSMSVirtualDeviceManager/Classes/ClassKeyboardHook.vb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,35 @@ Public Class ClassKeyboardHook
6969
End Sub
7070

7171
Public Function AnyButtonDown() As Boolean
72-
Return (g_iCurrentKeys.Count > 0)
72+
SyncLock g_mThreadLock
73+
Return (g_iCurrentKeys.Count > 0)
74+
End SyncLock
7375
End Function
7476

7577
Public Function GetButtonDown() As Keys()
76-
Return g_iCurrentKeys.ToArray
78+
SyncLock g_mThreadLock
79+
Return g_iCurrentKeys.ToArray
80+
End SyncLock
7781
End Function
7882

7983
Public Function IsButtonDown(iKey As Keys) As Boolean
80-
Return g_iCurrentKeys.Contains(iKey)
84+
SyncLock g_mThreadLock
85+
Return g_iCurrentKeys.Contains(iKey)
86+
End SyncLock
8187
End Function
8288

8389
Public Function IsButtonPressed(iKey As Keys) As Boolean
84-
Return (g_iCurrentKeys.Contains(iKey) AndAlso
85-
Not g_iLastKeys.Contains(iKey))
90+
SyncLock g_mThreadLock
91+
Return (g_iCurrentKeys.Contains(iKey) AndAlso
92+
Not g_iLastKeys.Contains(iKey))
93+
End SyncLock
8694
End Function
8795

8896
Public Function IsButtonReleased(iKey As Keys) As Boolean
89-
Return (Not g_iCurrentKeys.Contains(iKey) AndAlso
90-
g_iLastKeys.Contains(iKey))
97+
SyncLock g_mThreadLock
98+
Return (Not g_iCurrentKeys.Contains(iKey) AndAlso
99+
g_iLastKeys.Contains(iKey))
100+
End SyncLock
91101
End Function
92102

93103
Private Sub InstallHook()

PSMSVirtualDeviceManager/Classes/Tools/FormButtonInput.Designer.vb

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PSMSVirtualDeviceManager/Classes/Tools/FormButtonInput.vb

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
Private g_mThreadLock As New Object
77

88
Enum ENUM_DEVICE_TYPE
9-
KEYBOARD
10-
CONTROLLER
9+
KEYBOARD = (1 << 0)
10+
CONTROLLER = (1 << 1)
11+
ALL = &HFFFF
1112
End Enum
1213
Private g_iDeviceType As ENUM_DEVICE_TYPE = ENUM_DEVICE_TYPE.KEYBOARD
1314

@@ -22,17 +23,25 @@
2223
InitializeComponent()
2324

2425
' Add any initialization after the InitializeComponent() call.
26+
Dim sInfoText As New Text.StringBuilder
27+
sInfoText.AppendLine("Please press a button combination.")
2528

26-
Select Case (_DeviceType)
27-
Case ENUM_DEVICE_TYPE.KEYBOARD
28-
g_mKeyboardHook = New ClassKeyboardHook()
2929

30-
Case ENUM_DEVICE_TYPE.CONTROLLER
31-
For i = 0 To ClassControllerHook.ENUM_PLAYER_INDEX.__MAX - 1
32-
g_mControllerHook(i) = New ClassControllerHook(CType(i, ClassControllerHook.ENUM_PLAYER_INDEX))
33-
Next
30+
If ((g_iDeviceType And ENUM_DEVICE_TYPE.KEYBOARD) <> 0) Then
31+
g_mKeyboardHook = New ClassKeyboardHook()
3432

35-
End Select
33+
sInfoText.AppendLine(" - Waiting for keyboard input - ")
34+
End If
35+
36+
If ((g_iDeviceType And ENUM_DEVICE_TYPE.CONTROLLER) <> 0) Then
37+
For i = 0 To ClassControllerHook.ENUM_PLAYER_INDEX.__MAX - 1
38+
g_mControllerHook(i) = New ClassControllerHook(CType(i, ClassControllerHook.ENUM_PLAYER_INDEX))
39+
Next
40+
41+
sInfoText.AppendLine(" - Waiting for Xbox controller input - ")
42+
End If
43+
44+
Label_BindingInfo.Text = sInfoText.ToString
3645
End Sub
3746

3847
Private Sub FormButtonInput_Load(sender As Object, e As EventArgs) Handles MyBase.Load
@@ -42,60 +51,60 @@
4251
End Sub
4352

4453
Private Sub ThreadButtonDetection()
45-
Dim bHasPressed As Boolean = False
54+
Dim bHasKeyboardPressed As Boolean = False
55+
Dim bHasControllerPressed As Boolean = False
4656

4757
Try
4858
While True
4959
Try
50-
Select Case (m_DeviceType)
51-
Case ENUM_DEVICE_TYPE.KEYBOARD
52-
g_mKeyboardHook.Update()
60+
If ((m_DeviceType And ENUM_DEVICE_TYPE.KEYBOARD) <> 0) Then
61+
g_mKeyboardHook.Update()
62+
63+
If (g_mKeyboardHook.AnyButtonDown()) Then
64+
bHasKeyboardPressed = True
65+
66+
SyncLock g_mThreadLock
67+
For Each iKey In g_mKeyboardHook.GetButtonDown()
68+
g_iKeyboardKeys.Add(iKey)
69+
Next
70+
End SyncLock
71+
Else
72+
If (bHasKeyboardPressed) Then
73+
ClassUtils.AsyncInvoke(Sub()
74+
Me.DialogResult = DialogResult.OK
75+
Me.Close()
76+
End Sub)
77+
Return
78+
End If
79+
End If
80+
End If
81+
82+
If ((m_DeviceType And ENUM_DEVICE_TYPE.CONTROLLER) <> 0) Then
83+
For i = 0 To ClassControllerHook.ENUM_PLAYER_INDEX.__MAX - 1
84+
If (Not g_mControllerHook(i).IsConnected) Then
85+
Continue For
86+
End If
87+
88+
g_mControllerHook(i).Update()
5389

54-
If (g_mKeyboardHook.AnyButtonDown()) Then
55-
bHasPressed = True
90+
If (g_mControllerHook(i).AnyButtonDown()) Then
91+
bHasControllerPressed = True
5692

5793
SyncLock g_mThreadLock
58-
For Each iKey In g_mKeyboardHook.GetButtonDown()
59-
g_iKeyboardKeys.Add(iKey)
60-
Next
94+
g_iControllerIndex = CType(i, ClassControllerHook.ENUM_PLAYER_INDEX)
95+
g_iControllerKeys = (g_iControllerKeys Or g_mControllerHook(i).GetButtonDown)
6196
End SyncLock
6297
Else
63-
If (bHasPressed) Then
98+
If (bHasControllerPressed) Then
6499
ClassUtils.AsyncInvoke(Sub()
65100
Me.DialogResult = DialogResult.OK
66101
Me.Close()
67102
End Sub)
68103
Return
69104
End If
70105
End If
71-
72-
Case ENUM_DEVICE_TYPE.CONTROLLER
73-
For i = 0 To ClassControllerHook.ENUM_PLAYER_INDEX.__MAX - 1
74-
If (Not g_mControllerHook(i).IsConnected) Then
75-
Continue For
76-
End If
77-
78-
g_mControllerHook(i).Update()
79-
80-
If (g_mControllerHook(i).AnyButtonDown()) Then
81-
bHasPressed = True
82-
83-
SyncLock g_mThreadLock
84-
g_iControllerIndex = CType(i, ClassControllerHook.ENUM_PLAYER_INDEX)
85-
g_iControllerKeys = (g_iControllerKeys Or g_mControllerHook(i).GetButtonDown)
86-
End SyncLock
87-
Else
88-
If (bHasPressed) Then
89-
ClassUtils.AsyncInvoke(Sub()
90-
Me.DialogResult = DialogResult.OK
91-
Me.Close()
92-
End Sub)
93-
Return
94-
End If
95-
End If
96-
Next
97-
98-
End Select
106+
Next
107+
End If
99108
Catch ex As Threading.ThreadAbortException
100109
Throw
101110
Catch ex As Exception

0 commit comments

Comments
 (0)