Skip to content

Commit 8aeb8ed

Browse files
ssh1579traeagent
andauthored
feat: Check Project Technology Stack (#1)
Co-authored-by: traeagent <traeagent@users.noreply.github.com>
1 parent b605223 commit 8aeb8ed

3 files changed

Lines changed: 34 additions & 37 deletions

File tree

Controller2Mouse/Controller2Mouse.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,4 @@
1717
<Content Include="Controller2Mouse.ico" />
1818
</ItemGroup>
1919

20-
<ItemGroup>
21-
<PackageReference Include="SharpDX.XInput" Version="4.2.0" />
22-
</ItemGroup>
23-
2420
</Project>

Controller2Mouse/ControllerToMouseMapper.cs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
using SharpDX.XInput;
1+
using Windows.Gaming.Input;
22
using System.Windows.Threading;
33
using System.Diagnostics;
44

55
namespace GameController_Helper;
66

77
public class ControllerToMouseMapper
88
{
9-
private Controller? _controller;
9+
private Gamepad? _controller;
1010
private DispatcherTimer _timer;
11-
private const int Deadzone = 2500;
11+
private const double Deadzone = 2500.0 / 32768.0; // 对应原本的 2500 / 32768
1212
private const float SensitivityX = 0.0005f;
1313
private const float SensitivityY = 0.0005f;
1414
private const float ScrollSensitivity = 0.1f;
1515

16+
private const float MaxMoveSpeedX = 32768 * SensitivityX;
17+
private const float MaxMoveSpeedY = 32768 * SensitivityY;
18+
private const float MaxScrollSpeed = 255 * ScrollSensitivity;
19+
1620
private bool _isStarted;
1721

1822
private bool _leftStickDown;
@@ -22,7 +26,7 @@ public class ControllerToMouseMapper
2226

2327
public event Action<bool>? ConnectionStatusChanged;
2428

25-
public bool IsControllerConnected => _controller?.IsConnected ?? false;
29+
public bool IsControllerConnected => _controller != null;
2630

2731
public ControllerToMouseMapper()
2832
{
@@ -70,19 +74,17 @@ private void FindController()
7074
if ((DateTime.Now - _lastSearchTime).TotalSeconds < 1) return;
7175
_lastSearchTime = DateTime.Now;
7276

73-
var userIndices = new[] { UserIndex.One, UserIndex.Two, UserIndex.Three, UserIndex.Four };
74-
foreach (var index in userIndices)
77+
var gamepads = Gamepad.Gamepads;
78+
if (gamepads.Count > 0)
7579
{
76-
var c = new Controller(index);
77-
if (c.IsConnected)
78-
{
79-
_controller = c;
80-
ConnectionStatusChanged?.Invoke(true);
81-
return;
82-
}
80+
_controller = gamepads[0];
81+
ConnectionStatusChanged?.Invoke(true);
82+
}
83+
else
84+
{
85+
_controller = null;
86+
ConnectionStatusChanged?.Invoke(false);
8387
}
84-
_controller = null;
85-
ConnectionStatusChanged?.Invoke(false);
8688
}
8789

8890
private float _accumulatedX;
@@ -91,29 +93,28 @@ private void FindController()
9193

9294
private void Timer_Tick(object? sender, EventArgs e)
9395
{
94-
if (_controller == null || !_controller.IsConnected)
96+
if (_controller == null || !Gamepad.Gamepads.Contains(_controller))
9597
{
9698
FindController();
9799
return;
98100
}
99101

100102
if (!_isStarted) return;
101103

102-
var state = _controller.GetState();
103-
var gamepad = state.Gamepad;
104+
var reading = _controller.GetCurrentReading();
104105

105106
// 鼠标移动 (右摇杆)
106107
float targetX = 0;
107108
float targetY = 0;
108109

109-
if (Math.Abs((int)gamepad.RightThumbX) > Deadzone)
110+
if (Math.Abs(reading.RightThumbstickX) > Deadzone)
110111
{
111-
targetX = gamepad.RightThumbX * SensitivityX;
112+
targetX = (float)(reading.RightThumbstickX * MaxMoveSpeedX);
112113
}
113114

114-
if (Math.Abs((int)gamepad.RightThumbY) > Deadzone)
115+
if (Math.Abs(reading.RightThumbstickY) > Deadzone)
115116
{
116-
targetY = -gamepad.RightThumbY * SensitivityY; // Y轴反转
117+
targetY = (float)(-reading.RightThumbstickY * MaxMoveSpeedY); // Y轴反转
117118
}
118119

119120
_accumulatedX += targetX;
@@ -130,8 +131,8 @@ private void Timer_Tick(object? sender, EventArgs e)
130131
}
131132

132133
// 滚轮 (LT/RT) 与 中键 (LT+RT)
133-
bool ltPressed = gamepad.LeftTrigger > 128;
134-
bool rtPressed = gamepad.RightTrigger > 128;
134+
bool ltPressed = reading.LeftTrigger > 0.5; // 对应 > 128
135+
bool rtPressed = reading.RightTrigger > 0.5;
135136

136137
if (ltPressed && rtPressed)
137138
{
@@ -151,14 +152,14 @@ private void Timer_Tick(object? sender, EventArgs e)
151152

152153
float scrollAmount = 0;
153154
// LT 向上滚动 (正值), RT 向下滚动 (负值)
154-
if (gamepad.LeftTrigger > 30) // 设定触发死区
155+
if (reading.LeftTrigger > 0.117) // 对应 > 30 / 255
155156
{
156-
scrollAmount += gamepad.LeftTrigger * ScrollSensitivity;
157+
scrollAmount += (float)(reading.LeftTrigger * MaxScrollSpeed);
157158
}
158159

159-
if (gamepad.RightTrigger > 30)
160+
if (reading.RightTrigger > 0.117)
160161
{
161-
scrollAmount -= gamepad.RightTrigger * ScrollSensitivity;
162+
scrollAmount -= (float)(reading.RightTrigger * MaxScrollSpeed);
162163
}
163164

164165
if (Math.Abs(scrollAmount) > 0)
@@ -178,8 +179,8 @@ private void Timer_Tick(object? sender, EventArgs e)
178179
}
179180

180181
// 鼠标按键 (L3/R3) 与 虚拟键盘 (L3+R3)
181-
bool isLeftThumbPressed = (gamepad.Buttons & GamepadButtonFlags.LeftThumb) != 0;
182-
bool isRightThumbPressed = (gamepad.Buttons & GamepadButtonFlags.RightThumb) != 0;
182+
bool isLeftThumbPressed = reading.Buttons.HasFlag(GamepadButtons.LeftThumbstick);
183+
bool isRightThumbPressed = reading.Buttons.HasFlag(GamepadButtons.RightThumbstick);
183184

184185
if (isLeftThumbPressed && isRightThumbPressed)
185186
{

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ The published executable will be in `bin/Release/net11.0-windows10.0.26100.0/win
6666

6767
Advanced users can modify the following constants in [`ControllerToMouseMapper.cs`](Controller2Mouse/ControllerToMouseMapper.cs):
6868

69-
- `Deadzone`: Controller stick deadzone threshold (default: 2500)
69+
- `Deadzone`: Controller stick deadzone threshold
7070
- `SensitivityX`: Horizontal mouse sensitivity (default: 0.0005f)
7171
- `SensitivityY`: Vertical mouse sensitivity (default: 0.0005f)
7272
- `ScrollSensitivity`: Scroll wheel sensitivity (default: 0.1f)
7373

7474
## Technology Stack
7575

7676
- **Framework**: .NET 11.0 with WPF
77-
- **Input Library**: SharpDX.XInput 4.2.0
77+
- **Input Library**: Windows.Gaming.Input (Built-in WinRT API)
7878
- **Platform**: Windows 10/11
7979

8080
## Development
@@ -129,5 +129,5 @@ This project is provided as-is for educational and personal use.
129129

130130
## Acknowledgments
131131

132-
- Built with [SharpDX](https://github.com/sharpdx/SharpDX) for XInput support
132+
- Built with Windows.Gaming.Input for modern gamepad support
133133
- Uses Windows SendInput API for mouse simulation

0 commit comments

Comments
 (0)