Skip to content

Commit 275ce48

Browse files
committed
Depend on GetCursorPos to detect SAS
1 parent d05cd2a commit 275ce48

File tree

6 files changed

+126
-141
lines changed

6 files changed

+126
-141
lines changed

RELEASE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@
2323
- DEBUG allows to keep X360 controller always connected
2424
- Swap `STEAM+Menu` and `STEAM+Options`. It makes more sense to switch windows with STEAM+3 horizontal lines
2525
- If application is run with `-run-on-startup` it will self-set to run on system start
26+
- Depend on `GetCursorPos` to detect `SAS`

SteamController/Context.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public override string ToString()
4141
}
4242

4343
public bool RequestEnable { get; set; } = true;
44-
public bool KeyboardMouseValid { get; set; } = true;
4544
public ContextState State;
4645

4746
public event Action<Profiles.Profile> ProfileChanged;
@@ -52,6 +51,11 @@ public bool Enabled
5251
get { return RequestEnable; }
5352
}
5453

54+
public bool KeyboardMouseValid
55+
{
56+
get { return SteamController.Managers.SASManager.Valid; }
57+
}
58+
5559
public Profiles.Profile? CurrentProfile
5660
{
5761
get

SteamController/ContextDebug.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ public void Debug()
2020
items.Add("[LB]");
2121
if (Steam.LizardMouse)
2222
items.Add("[LM]");
23-
if (X360.Connected)
24-
items.Add("[X360]");
25-
else if (X360.Valid)
26-
items.Add("[no-X360]");
23+
24+
items.Add(X360.Connected ? "[X360]" : X360.Valid ? "[no-X360]" : "[inv-X360]");
25+
items.Add(KeyboardMouseValid ? "[KM]" : "[inv-KM]");
2726

2827
foreach (var button in Steam.AllButtons)
2928
{

SteamController/Devices/KeyboardController.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,32 @@ internal void BeforeUpdate()
8686
keyCodes = new Dictionary<VirtualKeyCode, DateTime>();
8787
}
8888

89+
private void Safe(Action action)
90+
{
91+
try
92+
{
93+
action();
94+
95+
Managers.SASManager.Valid = true;
96+
}
97+
catch (InvalidOperationException)
98+
{
99+
Managers.SASManager.Valid = false;
100+
}
101+
}
102+
89103
internal void Update()
90104
{
91105
// Key Up: it is missing now
92106
foreach (var keyUp in lastKeyCodes.Except(keyCodes))
93107
{
94-
try { simulator.Keyboard.KeyUp(keyUp.Key); }
95-
catch (InvalidOperationException) { }
108+
Safe(() => simulator.Keyboard.KeyUp(keyUp.Key));
96109
}
97110

98111
// Key Down: new keys being down
99112
foreach (var keyDown in keyCodes.Except(lastKeyCodes))
100113
{
101-
try { simulator.Keyboard.KeyDown(keyDown.Key); }
102-
catch (InvalidOperationException) { }
114+
Safe(() => simulator.Keyboard.KeyDown(keyDown.Key));
103115
}
104116

105117
// Key Repeats
@@ -109,29 +121,24 @@ internal void Update()
109121
if (keyPress.Value > now)
110122
continue;
111123

112-
try { simulator.Keyboard.KeyPress(keyPress.Key); }
113-
catch (InvalidOperationException) { }
114-
124+
Safe(() => simulator.Keyboard.KeyPress(keyPress.Key));
115125
keyCodes[keyPress.Key] = DateTime.Now.Add(NextRepeats);
116126
}
117127
}
118128

119129
public void KeyPress(params VirtualKeyCode[] keyCodes)
120130
{
121-
try { simulator.Keyboard.KeyPress(keyCodes); }
122-
catch (InvalidOperationException) { }
131+
Safe(() => simulator.Keyboard.KeyPress(keyCodes));
123132
}
124133

125134
public void KeyPress(VirtualKeyCode modifierKey, params VirtualKeyCode[] keyCodes)
126135
{
127-
try { simulator.Keyboard.ModifiedKeyStroke(modifierKey, keyCodes); }
128-
catch (InvalidOperationException) { }
136+
Safe(() => simulator.Keyboard.ModifiedKeyStroke(modifierKey, keyCodes));
129137
}
130138

131139
public void KeyPress(IEnumerable<VirtualKeyCode> modifierKeys, params VirtualKeyCode[] keyCodes)
132140
{
133-
try { simulator.Keyboard.ModifiedKeyStroke(modifierKeys, keyCodes); }
134-
catch (InvalidOperationException) { }
141+
Safe(() => simulator.Keyboard.ModifiedKeyStroke(modifierKeys, keyCodes));
135142
}
136143
}
137144
}

SteamController/Devices/MouseController.cs

Lines changed: 84 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using WindowsInput;
2-
using static CommonHelpers.Log;
32

43
namespace SteamController.Devices
54
{
@@ -77,14 +76,17 @@ public void Dispose()
7776
{
7877
}
7978

80-
private void Safe(Func<bool> action)
79+
private void Safe(Action action)
8180
{
8281
try
8382
{
8483
action();
84+
85+
Managers.SASManager.Valid = true;
8586
}
8687
catch (InvalidOperationException)
8788
{
89+
Managers.SASManager.Valid = false;
8890
}
8991
}
9092

@@ -103,67 +105,55 @@ internal void Update()
103105
// Mouse Up: it is missing now
104106
foreach (var button in lastMouseButtons.Except(mouseButtons))
105107
{
106-
Safe(() =>
108+
switch (button)
107109
{
108-
switch (button)
109-
{
110-
case Button.Left:
111-
simulator.Mouse.LeftButtonUp();
112-
return true;
113-
114-
case Button.Right:
115-
simulator.Mouse.RightButtonUp();
116-
return true;
117-
118-
case Button.Middle:
119-
simulator.Mouse.MiddleButtonUp();
120-
return true;
121-
122-
case Button.X:
123-
simulator.Mouse.XButtonUp(XButtonID);
124-
return true;
125-
126-
case Button.Y:
127-
simulator.Mouse.XButtonUp(YButtonID);
128-
return true;
129-
130-
default:
131-
return false;
132-
}
133-
});
110+
case Button.Left:
111+
Safe(() => simulator.Mouse.LeftButtonUp());
112+
break;
113+
114+
case Button.Right:
115+
Safe(() => simulator.Mouse.RightButtonUp());
116+
break;
117+
118+
case Button.Middle:
119+
Safe(() => simulator.Mouse.MiddleButtonUp());
120+
break;
121+
122+
case Button.X:
123+
Safe(() => simulator.Mouse.XButtonUp(XButtonID));
124+
break;
125+
126+
case Button.Y:
127+
Safe(() => simulator.Mouse.XButtonUp(YButtonID));
128+
break;
129+
}
134130
}
135131

136132
// Key Down: new keys being down
137133
foreach (var button in mouseButtons.Except(lastMouseButtons))
138134
{
139-
Safe(() =>
135+
switch (button)
140136
{
141-
switch (button)
142-
{
143-
case Button.Left:
144-
simulator.Mouse.LeftButtonDown();
145-
return true;
146-
147-
case Button.Right:
148-
simulator.Mouse.RightButtonDown();
149-
return true;
150-
151-
case Button.Middle:
152-
simulator.Mouse.MiddleButtonDown();
153-
return true;
154-
155-
case Button.X:
156-
simulator.Mouse.XButtonDown(XButtonID);
157-
return true;
158-
159-
case Button.Y:
160-
simulator.Mouse.XButtonDown(YButtonID);
161-
return true;
162-
163-
default:
164-
return false;
165-
}
166-
});
137+
case Button.Left:
138+
Safe(() => simulator.Mouse.LeftButtonDown());
139+
break;
140+
141+
case Button.Right:
142+
Safe(() => simulator.Mouse.RightButtonDown());
143+
break;
144+
145+
case Button.Middle:
146+
Safe(() => simulator.Mouse.MiddleButtonDown());
147+
break;
148+
149+
case Button.X:
150+
Safe(() => simulator.Mouse.XButtonDown(XButtonID));
151+
break;
152+
153+
case Button.Y:
154+
Safe(() => simulator.Mouse.XButtonDown(YButtonID));
155+
break;
156+
}
167157
}
168158

169159
// Move cursor
@@ -173,11 +163,7 @@ internal void Update()
173163
int y = movedY.Consume();
174164
if (x != 0 || y != 0)
175165
{
176-
Safe(() =>
177-
{
178-
simulator.Mouse.MoveMouseBy(x, y);
179-
return true;
180-
});
166+
Safe(() => simulator.Mouse.MoveMouseBy(x, y));
181167
}
182168
}
183169

@@ -187,11 +173,7 @@ internal void Update()
187173
int value = verticalScroll.Consume();
188174
if (value != 0)
189175
{
190-
Safe(() =>
191-
{
192-
simulator.Mouse.VerticalScroll(value);
193-
return true;
194-
});
176+
Safe(() => simulator.Mouse.VerticalScroll(value));
195177
}
196178
}
197179

@@ -200,77 +182,61 @@ internal void Update()
200182
int value = horizontalScroll.Consume();
201183
if (value != 0)
202184
{
203-
Safe(() =>
204-
{
205-
simulator.Mouse.HorizontalScroll(value);
206-
return true;
207-
});
185+
Safe(() => simulator.Mouse.HorizontalScroll(value));
208186
}
209187
}
210188
}
211189

212190
public void MouseClick(Button button)
213191
{
214-
Safe(() =>
192+
switch (button)
215193
{
216-
switch (button)
217-
{
218-
case Button.Left:
219-
simulator.Mouse.LeftButtonClick();
220-
return true;
194+
case Button.Left:
195+
Safe(() => simulator.Mouse.LeftButtonClick());
196+
break;
221197

222-
case Button.Right:
223-
simulator.Mouse.RightButtonClick();
224-
return true;
198+
case Button.Right:
199+
Safe(() => simulator.Mouse.RightButtonClick());
200+
break;
225201

226-
case Button.Middle:
227-
simulator.Mouse.MiddleButtonClick();
228-
return true;
202+
case Button.Middle:
203+
Safe(() => simulator.Mouse.MiddleButtonClick());
204+
break;
229205

230-
case Button.X:
231-
simulator.Mouse.XButtonClick(XButtonID);
232-
return true;
233-
234-
case Button.Y:
235-
simulator.Mouse.XButtonClick(YButtonID);
236-
return true;
206+
case Button.X:
207+
Safe(() => simulator.Mouse.XButtonClick(XButtonID));
208+
break;
237209

238-
default:
239-
return false;
240-
}
241-
});
210+
case Button.Y:
211+
Safe(() => simulator.Mouse.XButtonClick(YButtonID));
212+
break;
213+
}
242214
}
243215

244216
public void MouseDoubleClick(Button button)
245217
{
246-
Safe(() =>
218+
switch (button)
247219
{
248-
switch (button)
249-
{
250-
case Button.Left:
251-
simulator.Mouse.LeftButtonDoubleClick();
252-
return true;
220+
case Button.Left:
221+
Safe(() => simulator.Mouse.LeftButtonDoubleClick());
222+
break;
253223

254-
case Button.Right:
255-
simulator.Mouse.RightButtonDoubleClick();
256-
return true;
224+
case Button.Right:
225+
Safe(() => simulator.Mouse.RightButtonDoubleClick());
226+
break;
257227

258-
case Button.Middle:
259-
simulator.Mouse.MiddleButtonDoubleClick();
260-
return true;
228+
case Button.Middle:
229+
Safe(() => simulator.Mouse.MiddleButtonDoubleClick());
230+
break;
261231

262-
case Button.X:
263-
simulator.Mouse.XButtonDoubleClick(XButtonID);
264-
return true;
232+
case Button.X:
233+
Safe(() => simulator.Mouse.XButtonDoubleClick(XButtonID));
234+
break;
265235

266-
case Button.Y:
267-
simulator.Mouse.XButtonDoubleClick(YButtonID);
268-
return true;
269-
270-
default:
271-
return false;
272-
}
273-
});
236+
case Button.Y:
237+
Safe(() => simulator.Mouse.XButtonDoubleClick(YButtonID));
238+
break;
239+
}
274240
}
275241

276242
public void MoveBy(double pixelDeltaX, double pixelDeltaY)
@@ -281,11 +247,7 @@ public void MoveBy(double pixelDeltaX, double pixelDeltaY)
281247

282248
public void MoveTo(double absoluteX, double absoluteY)
283249
{
284-
Safe(() =>
285-
{
286-
simulator.Mouse.MoveMouseTo(absoluteX, absoluteY);
287-
return true;
288-
});
250+
Safe(() => simulator.Mouse.MoveMouseTo(absoluteX, absoluteY));
289251
}
290252

291253
public void VerticalScroll(double scrollAmountInClicks)

0 commit comments

Comments
 (0)