Same problem here, guys...
This is really annoying! Here the problem on the reseting the taskbar state is related to the regedit path: Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced on the key MMTaskbarMode.
Here I have an HDMI KVM, and everytime when I change the monitors, this configuration it's lost, but... Without lose the value on the key.
I've did a .bat to reset the explorer.
I'm looking for a solution since November, 2024.
Whoever, this week I think i've found a solution with Auto Hotkey.
On my situation, the problem occurs everytime when I activate/deactivate the KVM. And the solution was a script on the AHK, to restart the explorer, when the system notice the change on the USB conection from the KVM.
For who wants another method, just use the copilot or another IA to make changes on the code, its possible to trigger some other changes, like the number of displays, or the screen resolution to restart the "explorer.exe" and correct the taskbar.
The code:
#Persistent
#NoTrayIcon
WMI := ComObjGet("winmgmts:\.\root\CIMV2")
query := "SELECT * FROM __InstanceOperationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_USBControllerDevice'"
eventSink := WMI.ExecNotificationQuery(query)
Loop
{
event := eventSink.NextEvent()
devicePath := event.TargetInstance.Dependent
; Verifica se o dispositivo reconectado é sua KVM (ID USB específico)
if (InStr(devicePath, "VID\_214B&PID\_7250"))
{
; Aguarda um pouco para estabilizar
Sleep 3000
; Ajusta displays (força interno, depois estende)
RunWait, %ComSpec% /c "C:\Windows\System32\DisplaySwitch.exe /internal",, Hide
Sleep 2000
RunWait, %ComSpec% /c "C:\Windows\System32\DisplaySwitch.exe /extend",, Hide
Sleep 2000
; Reinicia explorer com segurança
Process, Close, explorer.exe
Sleep 2000
DllCall("Shell32\SHRestartShell")
; Força atualização visual
DllCall("user32.dll\UpdatePerUserSystemParameters", "Int", 1)
; Loga sucesso
FormatTime, now,, yyyy-MM-dd HH:mm:ss
FileAppend, [%now%] Dispositivo KVM reconectado - Correção executada com sucesso`n, C:\Temp\kvm\_usb\_watchdog.log
}
}
Ps.: Sorry about the bad english, and the commentaries on the code are on Brazilian Portuguese.