From 1e6f8c0b9f6ab6e46e712b6851faf17092d5b118 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gerard=20Gasc=C3=B3n?=
<52170489+GerardGascon@users.noreply.github.com>
Date: Fri, 28 Jun 2024 14:23:17 +0200
Subject: [PATCH] refactor: extract input sender to an interface for multiple
platform support
---
.../.idea/workspace.xml | 4 ++
SlidePresenter/InputSender/IInputSender.cs | 6 +++
.../InputSender/WindowsInputSender.cs | 47 +++++++++++++++++
SlidePresenter/SlideSwitcher.cs | 51 ++-----------------
4 files changed, 61 insertions(+), 47 deletions(-)
create mode 100644 SlidePresenter/InputSender/IInputSender.cs
create mode 100644 SlidePresenter/InputSender/WindowsInputSender.cs
diff --git a/.idea/.idea.SwitchSlidePresenter/.idea/workspace.xml b/.idea/.idea.SwitchSlidePresenter/.idea/workspace.xml
index 6d4b518..41f33f2 100644
--- a/.idea/.idea.SwitchSlidePresenter/.idea/workspace.xml
+++ b/.idea/.idea.SwitchSlidePresenter/.idea/workspace.xml
@@ -37,11 +37,15 @@
+
+
+
{
"associatedIndex": 6
}
+
diff --git a/SlidePresenter/InputSender/IInputSender.cs b/SlidePresenter/InputSender/IInputSender.cs
new file mode 100644
index 0000000..e0bce4e
--- /dev/null
+++ b/SlidePresenter/InputSender/IInputSender.cs
@@ -0,0 +1,6 @@
+namespace SwitchSlidePresenter.InputSender;
+
+public interface IInputSender {
+ void NextSlide();
+ void PreviousSlide();
+}
\ No newline at end of file
diff --git a/SlidePresenter/InputSender/WindowsInputSender.cs b/SlidePresenter/InputSender/WindowsInputSender.cs
new file mode 100644
index 0000000..bcde5ed
--- /dev/null
+++ b/SlidePresenter/InputSender/WindowsInputSender.cs
@@ -0,0 +1,47 @@
+using System.Runtime.InteropServices;
+using Win32Api;
+
+namespace SwitchSlidePresenter.InputSender;
+
+public class WindowsInputSender : IInputSender {
+ private const uint INPUT_KEYBOARD = 1;
+ private const ushort VK_NEXT = 0x22;
+ private const ushort VK_PRIOR = 0x21;
+ private const uint KEYEVENTF_KEYDOWN = 0x0000;
+ private const uint KEYEVENTF_KEYUP = 0x0002;
+
+ public void NextSlide() => SimulateKeyPress(VK_NEXT);
+ public void PreviousSlide() => SimulateKeyPress(VK_PRIOR);
+
+ private static void SimulateKeyPress(ushort keyCode) {
+ Input[] inputs = new Input[2];
+
+ inputs[0] = new Input {
+ type = INPUT_KEYBOARD,
+ u = new InputUnion {
+ ki = new KeyboardInput {
+ wVk = keyCode,
+ wScan = 0,
+ dwFlags = KEYEVENTF_KEYDOWN,
+ time = 0,
+ dwExtraInfo = IntPtr.Zero
+ }
+ }
+ };
+
+ inputs[1] = new Input {
+ type = INPUT_KEYBOARD,
+ u = new InputUnion {
+ ki = new KeyboardInput {
+ wVk = keyCode,
+ wScan = 0,
+ dwFlags = KEYEVENTF_KEYUP,
+ time = 0,
+ dwExtraInfo = IntPtr.Zero
+ }
+ }
+ };
+
+ Win32Api.Win32Api.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(Input)));
+ }
+}
\ No newline at end of file
diff --git a/SlidePresenter/SlideSwitcher.cs b/SlidePresenter/SlideSwitcher.cs
index d81adae..004c51a 100644
--- a/SlidePresenter/SlideSwitcher.cs
+++ b/SlidePresenter/SlideSwitcher.cs
@@ -1,16 +1,10 @@
-using System.Runtime.InteropServices;
-using Win32Api;
+using SwitchSlidePresenter.InputSender;
namespace SwitchSlidePresenter;
public class SlideSwitcher : IDisposable {
private readonly IGamepadReader? _reader;
-
- private const uint INPUT_KEYBOARD = 1;
- private const ushort VK_NEXT = 0x22;
- private const ushort VK_PRIOR = 0x21;
- private const uint KEYEVENTF_KEYDOWN = 0x0000;
- private const uint KEYEVENTF_KEYUP = 0x0002;
+ private readonly IInputSender _inputSender = new WindowsInputSender();
public SlideSwitcher(IGamepadReader? reader) {
_reader = reader;
@@ -23,43 +17,6 @@ public class SlideSwitcher : IDisposable {
_reader.PrevSlide -= PreviousSlide;
}
- private static void NextSlide() {
- SimulateKeyPress(VK_NEXT);
- }
-
- private static void PreviousSlide() {
- SimulateKeyPress(VK_PRIOR);
- }
-
- private static void SimulateKeyPress(ushort keyCode) {
- Input[] inputs = new Input[2];
-
- inputs[0] = new Input {
- type = INPUT_KEYBOARD,
- u = new InputUnion {
- ki = new KeyboardInput {
- wVk = keyCode,
- wScan = 0,
- dwFlags = KEYEVENTF_KEYDOWN,
- time = 0,
- dwExtraInfo = IntPtr.Zero
- }
- }
- };
-
- inputs[1] = new Input {
- type = INPUT_KEYBOARD,
- u = new InputUnion {
- ki = new KeyboardInput {
- wVk = keyCode,
- wScan = 0,
- dwFlags = KEYEVENTF_KEYUP,
- time = 0,
- dwExtraInfo = IntPtr.Zero
- }
- }
- };
-
- Win32Api.Win32Api.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(Input)));
- }
+ private void NextSlide() => _inputSender.NextSlide();
+ private void PreviousSlide() => _inputSender.PreviousSlide();
}
\ No newline at end of file