Saturday, August 30, 2025

2025-08-30 - Mouse Without Borders network refresh script

 

Automating Mouse Without Borders Refresh with AutoHotkey and PowerToys

I ran into a recurring problem with Mouse Without Borders (MWB) running through PowerToys on my desktop. My laptop is usually controlling the mouse and keyboard, and the desktop often stops responding to MWB. I wanted a way to refresh the connections without having to physically go to the desktop and press the button every time.

I initially considered the possibility of running a batch or PowerShell script that would just open Mouse Without Borders, and my research kept showing this could totally be done but kept running into issues and conflicting information before I found that there are two versions of MWB that I am aware of, one that is installed through Garage, I guess it's called, and the one through PowerToys. And this version is not designed to be managed through the CLI.

I tried using AutoHotkey (AHK) to automate opening PowerToys, navigating to MWB, and pressing the “Refresh Connections” button. The first approach involved sending keyboard inputs by pressing Control + Tab, which didn't work. I tried manually pressing these keys myself and found that just pressing Tab and then Down Arrow would reach the MWB window so the script was adjusted to:

  • WinActivate to focus PowerToys.

  • Tabbing 11 times to reach the modules window.

  • Arrowing down 16 times to highlight Mouse Without Borders.

  • Pressing Enter to open it.

  • Using ControlClick to hit the refresh button.

I added #Persistent to keep the script running long enough to complete the actions, and #SingleInstance Force to prevent multiple instances from running simultaneously. Sleep commands like Sleep, 500 and Sleep, 2000 were critical—they gave PowerToys time to fully load and respond. Otherwise, the tabs and arrows would be sent too quickly, and the actions wouldn’t land.

Despite all this, it never worked despite multiple changes to the sleep timers, even though manually pressing those keys worked fine. I also had to deal with scripts asking for admin privileges because my VPN setup made MWB refuse some actions unless PowerToys was elevated. At first, I tried adding Run *RunAs to make the script itself launch PowerToys as admin. Then there was the suggestion to run AutoHotkey itself as admin too. But I didn’t like stacking admin requirements on top of each other—it felt too risky. I decided to back away from that approach because admin should be used sparingly if possible, and I wanted a solution that didn’t require giving Santa’s mailman admin rights to my computer just to keep one simple thing running consistently. 

I also wanted the script to exit automatically once it finished, because once PowerToys was opened and MWB refreshed, there was no reason to keep the script running. I added a 30-second Sleep, 30000 followed by ExitApp, ensuring the script completed the refresh and then stopped. That way, future runs of the script would always start fresh and simply refresh MWB again.

Testing the script required ending it manually sometimes and even then it was finicky for reasons I didn't look into in order to avoid yak shaving. In Task Manager, I looked for AutoHotkey Unicode 64-bit and ended it so it was totally ended so I could continue testing but didn't eliminate the problem. 

Even with the tab/arrow method, I realized it was brittle. PowerToys can behave differently depending on which modules are running, and the window focus can change. After testing in vain many times, I wondered if maybe just freshly opening PowerToys would also refresh the connection. I was thinking of forcefully restarting PowerToys to quickly refresh MWB connections but thought it might cause errors eventually if it was forced closed every single time it was closed just to be reopened. Research showed that it might have errors if changes were made occasionally and it didn't close properly and files might get corrupted. I then looked to see how long it would take it to close normally considering how many modules in PowerToys I use, which is all but six of them, and as far as I could tell, it was only about 2-5 seconds. So I thought this would possibly be invisible enough if I had it running automatically through Task Scheduler to run off of network interruption triggers (which I still need to explore). So I landed on this approach:

  • Close PowerToys using WinClose and wait for it to fully exit using WinWaitClose.

  • If it doesn’t close in 5 seconds, fall back to Process, Close as a last resort.

  • Reopen PowerToys normally (no admin needed).

Timing is important. Most modules load quickly, and with only six active, a 2–3 second wait after reopening PowerToys is sufficient to ensure MWB is ready. This approach eliminates the need for tabbing, arrows, or clicks, and avoids issues with window size or focus. Also timing matters for when the script ends. I don't want it to end before it has had a chance to finish running, and to end soon enough that if I need to run it again, I don't have to wait too terribly long for the script to end automatically so I set it to 30 seconds. 

The script did look like this: 

#Persistent

#SingleInstance Force


; Launch PowerToys if not already running

IfWinNotExist, ahk_class ApplicationFrameWindow

{

    Run, "C:\Program Files\PowerToys\PowerToys.exe"

    WinWait, ahk_class ApplicationFrameWindow

}


; Activate PowerToys window and wait for it to fully load

WinActivate, ahk_class ApplicationFrameWindow

Sleep, 2000  ; 2 seconds to ensure window is ready


; Navigate to the modules window

Send, {Tab 11}

Sleep, 1000  ; wait 1 second before navigating down


; Navigate to Mouse Without Borders

Send, {Down 16}

Sleep, 500


; Open Mouse Without Borders

Send, {Enter}

Sleep, 500


; Click the Refresh Connections button

ControlClick, InputSiteWindowClass1, ahk_class WinUIDesktopWin32WindowClass, , , , NA


; Wait 30 seconds to ensure refresh completes

Sleep, 30000


; Exit the AutoHotkey script

ExitApp

And then I set it to look like this when I decided to change to the simpler open and close app method:

# Launch-PowerToys.ps1


# Function: Start PowerToys with admin

function Start-PowerToys {

    Start-Process -FilePath "C:\Program Files\PowerToys\PowerToys.exe" -Verb RunAs

}


# Start PowerToys on boot

Start-PowerToys


# Create a watcher for network disruption

$job = Register-ObjectEvent -InputObject ([System.Net.NetworkInformation.NetworkChange]) `

    -EventName "NetworkAvailabilityChanged" `

    -Action {

        Start-PowerToys

    }


# Keep script alive for 30 seconds so the event watcher works

Start-Sleep -Seconds 30


# Cleanup

Unregister-Event -SourceIdentifier $job.Name

I set up the main copy of the script to be in the Scripts folder in my C drive so it's out of the way and not haphazardly deleted. And then I went to it, right clicked and sent it to the desktop for last resort uses if triggers in task scheduler don't work for whatever reason. 

The combination of graceful shutdown (WinClose), a small fallback for forced closure, and timed waits ensures MWB always refreshes reliably. #SingleInstance Force prevents overlapping runs, and the script exits automatically so future triggers always start fresh.

In the end, this method proved much simpler and more reliable than trying to simulate tabs, arrow keys, or mouse clicks, and it works even when VPNs are active. It keeps MWB functional without requiring constant manual intervention or admin privileges, and the Task Scheduler triggers cover all the common cases that could disrupt connectivity.

This did not resolve the issue. I will have to return to it later as I have an appointment to keep. 


Now, this method using Auto Hot Key with Task Scheduler to manage Mouse Without Borders didn't work. So I tried another method. 

What did work for a time was when I finally installed Microsoft Garage Mouse Without Borders or MG MWB, having learned that there was a version before PowerToys where MWB was standalone, and this could be controlled with scripts. My impression was also that it could be controlled through the CLI. But I have not attempted this as of yet. 

What I did try was just installing MG MWB and it was confusing, not as wieldy as the PowerToys version, it was harder to connect my two computers, and it was resistant to recognize one or the other. Once I did get them connected, I assumed there was conflict with PowerToys and so I tried turning off PowerToys MWB, which did nothing so I went back to using PowerToys MWB instead. And then a few days later, it became apparent that MG MWB was actually working, and PowerToys MWB was only turned off on one computer even though I thought I had turned it off on both. Whatever. It was working now. So I turned it off on both computers. 

I set MG MWB to move the mouse between computers only when holding the control key as I had become accustomed to doing with the PowerToys version, and it wasn't very straightforward to do that either but it was possible. Initially research said I couldn't do this with the control key and then not at all, and I rolled my eyes and then again when I found the selections to do it and to do it with the control key as desired despite my research. 

And then I created a script to simply kill and restart MG MWB:

@echo off REM Terminate MWB processes taskkill /IM MouseWithoutBorders.exe /F taskkill /IM

 MouseWithoutBordersHelper.exe /F REM Wait for 10 seconds timeout /t 10 /nobreak REM Restart

 MWB start "" "C:\Program Files (x86)\Microsoft Garage\Mouse without

 Borders\MouseWithoutBorders.exe" REM Exit the batch script exit

What this did was it restarted MG MWB and took ten seconds to do it on purpose so that whatever happened, I could be sure that the tasks were killed, and there was more than one task so it was crucial they were all killed, so I was willing to wait at first for the ten seconds. After the ten seconds, UAC would ask to confirm if I wanted this app to change my computer. I would have to say yes. Many times this got frustrating because I would initiate the script and then watch as it counted down and then worked and it never seemed to actually need that much time, the effect was instant once the counter ran out, and so today I decided to simply decrease the timer even though the UAC part was what really annoyed me. But as long as I had to switch my mouse to controlling my desktop directly to activate the script, it wouldn't be too much extra effort to immediately then address the UAC confirmation. So I just edited the script to only count down from 5 instead of ten and decided to look into whether or not, spur of the moment, could it work without the AUC prompt and could it be lessened to even less than 5 seconds or eliminated altogether. 

With some research, I found that if I opened task scheduler with administrative privileges, created a task, named it, set it to run when logged into this account, no trigger needed since my script would be handling the trigger, point it to MG MWB, the .exe file in Program files x86 folder, then right clicking on it to run it from task schedueler to make sure it worked without UAC prompts, which it did, then I could eliminate the UAC altogether. The point of the task schedueler in this case isn't to actually activate a script at a certain triggered time or instant, but rather to use it conveniently to get around UAC since Windows will stop any task from making potentially serious changes without adequate permission. How can you do this, by using a tool built into Windows that Windows will not quibble over, which already has authority to bypass UAC. I have no idea, not being an expert at this, that UAC couldn't be bypassed other ways, and I have tried by getting both versions of MWB to run as administrator by going alt enter and changing their authority to administrator, but this didn't solve the problem. 

I tried to use my script to make it work and figured the script had to change, ti point to Task Scheduler now. So I created a new script:

@echo off

REM Kill MWB processes

taskkill /IM MouseWithoutBorders.exe /F >nul 2>&1

taskkill /IM MouseWithoutBordersHelper.exe /F >nul 2>&1


REM Give it a quick 2-second moment to exit

timeout /t 2 /nobreak >nul


REM Run the scheduled task (runs elevated without UAC prompt)

schtasks /Run /TN "MWB Restart" >nul 2>&1


exit /b 0

So far this works great. 


This has been Truncat3d 00000000111100010100110______________end of line

No comments:

Post a Comment

2026-01-02 - Installing PWA (Progressive Web App) in Chrome

      Back in March 2025, I started using Grok and wanted to add it to what I use AI for, and liked ChatGPT's Desktop app. I had used GP...