From c6d1b29a49654958a516e6a8d74a527c2905c0a0 Mon Sep 17 00:00:00 2001 From: Serge <5920850+seerge@users.noreply.github.com> Date: Mon, 23 Sep 2024 15:24:28 +0200 Subject: [PATCH] Touchscreen toggle toast tweaks https://github.com/seerge/g-helper/issues/3143 --- app/Helpers/TouchscreenHelper.cs | 20 +++++++++++++++----- app/Input/InputDispatcher.cs | 10 +++++++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/app/Helpers/TouchscreenHelper.cs b/app/Helpers/TouchscreenHelper.cs index aafb8d7b..07754b9a 100644 --- a/app/Helpers/TouchscreenHelper.cs +++ b/app/Helpers/TouchscreenHelper.cs @@ -2,21 +2,31 @@ public static class TouchscreenHelper { - public static bool? ToggleTouchscreen() + + public static bool? GetStatus() { try { ProcessHelper.RunAsAdmin(); + return ProcessHelper.RunCMD("powershell", "(Get-PnpDevice -FriendlyName '*touch*screen*').Status").Contains("OK"); + } + catch (Exception ex) + { + Logger.WriteLine($"Can't get touchscreen status: {ex.Message}"); + return null; + } + } - var status = !ProcessHelper.RunCMD("powershell", "(Get-PnpDevice -FriendlyName '*touch*screen*').Status").Contains("OK"); + public static void ToggleTouchscreen(bool status) + { + try + { + ProcessHelper.RunAsAdmin(); ProcessHelper.RunCMD("powershell", (status ? "Enable-PnpDevice" : "Disable-PnpDevice") + " -InstanceId (Get-PnpDevice -FriendlyName '*touch*screen*').InstanceId -Confirm:$false"); - - return status; } catch (Exception ex) { Logger.WriteLine($"Can't toggle touchscreen: {ex.Message}"); - return null; } } diff --git a/app/Input/InputDispatcher.cs b/app/Input/InputDispatcher.cs index 5658342b..9edad46a 100644 --- a/app/Input/InputDispatcher.cs +++ b/app/Input/InputDispatcher.cs @@ -591,9 +591,13 @@ namespace GHelper.Input Program.settingsForm.BeginInvoke(Program.settingsForm.allyControl.ToggleModeHotkey); break; case "touchscreen": - var touchscreenStatus = TouchscreenHelper.ToggleTouchscreen(); - if (touchscreenStatus is not null) - Program.toast.RunToast(Properties.Strings.Touchscreen + " " + ((bool)touchscreenStatus ? Properties.Strings.On : Properties.Strings.Off), ToastIcon.Touchpad); + var status = !TouchscreenHelper.GetStatus(); + Logger.WriteLine("Touchscreen status: " + status); + if (status is not null) + { + Program.toast.RunToast(Properties.Strings.Touchscreen + " " + ((bool)status ? Properties.Strings.On : Properties.Strings.Off), ToastIcon.Touchpad); + TouchscreenHelper.ToggleTouchscreen((bool)status); + } break; default: break;