From f6304fedff2ce29f6aec73f014bd86c0b7936469 Mon Sep 17 00:00:00 2001 From: Steve Williams Date: Sat, 28 Jan 2023 11:03:48 +0000 Subject: [PATCH] New scripts for reactivate window scenario. --- Utils/Activate Window.ps1 | 18 ++++++++++++++++++ Utils/Capture Active Window.ps1 | 28 ++++++++++++++++++++++++++++ Utils/Watch Idle TIme.ps1 | 4 +++- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 Utils/Activate Window.ps1 create mode 100644 Utils/Capture Active Window.ps1 diff --git a/Utils/Activate Window.ps1 b/Utils/Activate Window.ps1 new file mode 100644 index 0000000..24ef334 --- /dev/null +++ b/Utils/Activate Window.ps1 @@ -0,0 +1,18 @@ +# https://github.com/sw3103/movemouse/wiki/Scenarios#reactivate-previous-active-window-on-pausestop + +$TextFilePath = Join-Path -Path:$env:TEMP -ChildPath:"MoveMouseWindowPid.txt" + +if (Test-Path -Path:$TextFilePath) { + $WindowPid = Get-Content -Path:$TextFilePath + + if (![System.String]::IsNullOrWhiteSpace($WindowPid)) { + Add-Type -AssemblyName:"Microsoft.VisualBasic" + [Microsoft.VisualBasic.Interaction]::AppActivate([int]$WindowPid) + } + else { + throw "$TextFilePath was empty." + } +} +else { + throw "$TextFilePath does not exist. You need to run this script with the -Capture parameter first." +} \ No newline at end of file diff --git a/Utils/Capture Active Window.ps1 b/Utils/Capture Active Window.ps1 new file mode 100644 index 0000000..6ff8da1 --- /dev/null +++ b/Utils/Capture Active Window.ps1 @@ -0,0 +1,28 @@ +# https://github.com/sw3103/movemouse/wiki/Scenarios#reactivate-previous-active-window-on-pausestop + +Add-Type -TypeDefinition @" +using System; +using System.Runtime.InteropServices; + +public class NativeMethods +{ + [DllImport("user32.dll")] + public static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll", SetLastError=true)] + public static extern uint GetWindowThreadProcessId( + IntPtr hWnd, + out uint lpdwProcessId); +} +"@ + +$TextFilePath = Join-Path -Path:$env:TEMP -ChildPath:"MoveMouseWindowPid.txt" +$Hwnd = [NativeMethods]::GetForegroundWindow() +$WindowPid = 0 + +if ([NativeMethods]::GetWindowThreadProcessId($Hwnd, [ref]$WindowPid) -gt 0) { + $WindowPid | Out-File -FilePath:$TextFilePath -Append:$false -Force +} +else { + throw "Error capturing active window process ID." +} \ No newline at end of file diff --git a/Utils/Watch Idle TIme.ps1 b/Utils/Watch Idle TIme.ps1 index 7d38a64..1387951 100644 --- a/Utils/Watch Idle TIme.ps1 +++ b/Utils/Watch Idle TIme.ps1 @@ -1,4 +1,6 @@ -Add-Type @' +# https://github.com/sw3103/movemouse/wiki/Troubleshooting#idle-time + +Add-Type @' using System; using System.Diagnostics; using System.Runtime.InteropServices;