From 5f7bb79e56a57e766ff6e5d2284e1911f7c768bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Gasc=C3=B3n?= <52170489+GerardGascon@users.noreply.github.com> Date: Fri, 7 Jun 2024 00:08:43 +0200 Subject: [PATCH] feat: switching slides --- JoyConRead.cs | 5 +++-- Program.cs | 2 ++ SlideSwitcher.cs | 26 ++++++++++++++++++++++++++ Win32Api.cs | 14 ++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 SlideSwitcher.cs create mode 100644 Win32Api.cs diff --git a/JoyConRead.cs b/JoyConRead.cs index 95ed86d..4eb901b 100644 --- a/JoyConRead.cs +++ b/JoyConRead.cs @@ -29,7 +29,6 @@ public class JoyConRead : GamepadReader { await LogDeviceInfo(joycon); joycon.ReportReceived += OnJoyConOnReportReceived; - Console.WriteLine($"JoyCon ready for presenting."); joycon.StoppedOnError += (_, ex) => { Console.WriteLine(); @@ -39,7 +38,9 @@ public class JoyConRead : GamepadReader { return Task.CompletedTask; }; - Console.ReadKey(); + Console.WriteLine("JoyCon ready for presenting."); + Console.WriteLine("Press Enter to exit program."); + while (Console.ReadKey().Key != ConsoleKey.Enter) { } joycon.Stop(); Console.WriteLine(); diff --git a/Program.cs b/Program.cs index df88ad9..91aef77 100644 --- a/Program.cs +++ b/Program.cs @@ -2,7 +2,9 @@ class Program { private static async Task Main() { GamepadReader reader = new JoyConRead(); + SlideSwitcher switcher = new(reader); await reader.Read(); + switcher.Dispose(); } } } \ No newline at end of file diff --git a/SlideSwitcher.cs b/SlideSwitcher.cs new file mode 100644 index 0000000..837c292 --- /dev/null +++ b/SlideSwitcher.cs @@ -0,0 +1,26 @@ +namespace SwitchSlidePresenter; + +public class SlideSwitcher : IDisposable { + private readonly GamepadReader _reader; + + public SlideSwitcher(GamepadReader reader) { + _reader = reader; + _reader.NextSlide += NextSlide; + _reader.PrevSlide += PreviousSlide; + } + + public void Dispose() { + _reader.NextSlide -= NextSlide; + _reader.PrevSlide -= PreviousSlide; + } + + private void NextSlide() { + IntPtr handle = Win32Api.GetForegroundWindow(); + Win32Api.PostMessage(handle, Win32Api.WM_KEYDOWN, Win32Api.VK_NEXT, 0); + } + + private void PreviousSlide() { + IntPtr handle = Win32Api.GetForegroundWindow(); + Win32Api.PostMessage(handle, Win32Api.WM_KEYDOWN, Win32Api.VK_PRIOR, 0); + } +} \ No newline at end of file diff --git a/Win32Api.cs b/Win32Api.cs new file mode 100644 index 0000000..e5f503c --- /dev/null +++ b/Win32Api.cs @@ -0,0 +1,14 @@ +using System.Runtime.InteropServices; + +namespace SwitchSlidePresenter; + +public static class Win32Api { + public const UInt32 WM_KEYDOWN = 0x0100; + public const int VK_NEXT = 0x22; + public const int VK_PRIOR = 0x21; + + [DllImport("user32.dll")] + public static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam); + [DllImport("user32.dll")] + public static extern IntPtr GetForegroundWindow(); +} \ No newline at end of file