Toggling laptop touchscreen with a keyboard shortcut
Tags: tutorial, windows, powershell, touchscreen
Published: 2022-03-06 22:32:57

My college friends had a fascination with dragging their fingers across my laptop touchscreen. I wasn't so keen, and thus [Touchscreen Toggle](https://github.com/smcclennon/old-projects/blob/master/touchscreen_toggle) (TST) was created, originally a script which toggled my touchscreen with a keyboard shortcut.

This project was my first experience working with device drivers, and uses the Windows [devcon](https://github.com/Microsoft/Windows-driver-samples/tree/master/setup/devcon) utility to disable and re-enable a device driver.

I first used devcon to determine which Human Interface Device was responsible for my touchscreen, which in my case was `HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*`. Using `devcon disable` on this HID disabled my screens touch functionality, and using `devcon enable` restored the touch functionality.

## Toggle script
I created a small batch script which would toggle the driver based on the output from `devcon status`.
1. If the string `Driver is running.` was found, it would disable the driver.
2. If it instead failed to find that string, it would enable the driver.
```batch
devcon status "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*" | find /i "Driver is running.">nul
if not %errorlevel%==1 (
	devcon disable "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*"
) else (
	devcon enable "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*"
)
```
I saved the driver toggle script in [`C:\Windows\TST\TST.bat`](https://github.com/smcclennon/old-projects/blob/master/touchscreen_toggle/TST.bat).

## Keyboard shortcut
Having to run that script manually as an administrator was inconvenient and slow, by which time my friends would have reorganised my windows. I looked into running a script as an administrator with a keyboard shortcut and settled on using [AutoHotkey](https://www.autohotkey.com/).

Devcon required administrative privileges to run, so I set AutoHotkey to run and execute all scripts as administrator (`Properties -> Compatibility -> Run this program as administrator`).

I then created this AutoHotkey script to run it when I press the key combination `Right Alt` + `F12`.
```
﻿RAlt & F12::Run, C:\Windows\TST\TST.bat
```
I saved this AutoHotkey script as [`DriverToggle.ahk`](https://github.com/smcclennon/old-projects/blob/master/touchscreen_toggle/DriverToggle.ahk).

## Toast notifications
It was working perfectly, and I was able to quickly disable my touchscreen to mitigate surprise inbound fingers from disrupting my workflow.

It became a guessing game knowing when my touch screen was enabled though, so to make my script even fancier I looked into generating Windows notifications from the command line.

I found an [article](https://www.addictivetips.com/windows-tips/show-a-custom-toast-notification-on-windows-10/) showing how to achieve this in PowerShell using the [BurntToast](https://github.com/Windos/BurntToast) module.

My PowerShell `ExecutionPolicy` was set to `Restricted` which does not allow installing any remote modules, so I had to first [change my ExecutionPolicy](https://docs.microsoft.com/en-gb/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-6):
```powershell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
``` 
Next, I ran `Install-Module -Name BurntToast` to install BurntToast.

Finally, I added my logo to [`C:\Windows\TST\image.png`](https://github.com/smcclennon/old-projects/blob/master/touchscreen_toggle/image.png)) and edited the driver toggle script to generate toast notifications with my logo as the icon using `powershell New-BurntToastNotification`:
```batch
devcon status "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*" | find /i "Driver is running.">nul
if not %errorlevel%==1 (
	devcon disable "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*"
        powershell New-BurntToastNotification -Text 'Touch Screen Toggled', 'Your touch screen is now disabled.' -AppLogo image.png
) else (
	devcon enable "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*"
        powershell New-BurntToastNotification -Text 'Touch Screen Toggled', 'Your touch screen has been enabled.' -AppLogo image.png
)
```

## PowerShell script
I noticed that the toast notifications took a couple of seconds to appear, and instead of a black command prompt box appearing and closing immediately when running my driver toggle script, it would stay open till the toast notification appeared.

I also observed that running `powershell New-BurntToastNotification` from Command Prompt has the same delay, but running `New-BurntToastNotification` directly from PowerShell had no delay.

This led me to rewriting the driver toggle script in PowerShell:
```powershell
if(devcon status "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*" | find /i "Driver is running.") {
	if(devcon disable "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*" | find /i "Disable failed") {
        New-BurntToastNotification -Text "Touch Screen Toggle Failed", "Failed to disable your touch screen (Am I running with administrator permissions? Is your hardware ID correct?)" -AppLogo image.png
    } else {
        New-BurntToastNotification -Text "Touch Screen Toggled", "Your touch screen is now disabled." -AppLogo image.png
    }
} else {
	if(devcon enable "HID\VEN_8086&DEV_9D3E&SUBSYS_00000000&REV_21&COL05*" | find /i "Enable failed") {
        New-BurntToastNotification -Text "Touch Screen Toggle Failed", "Failed to enable your touch screen (Am I running with administrator permissions? Is your hardware ID correct?)" -AppLogo image.png
    } else {
        New-BurntToastNotification -Text 'Touch Screen Toggled', "Your touch screen has been enabled." -AppLogo image.png
    }
}
```
After changing my AutoHotkey script to point to this PowerShell script instead, I still observed the same delay. I concluded that the delay was PowerShell itself starting up, and my tests where PowerShell had no delay was due to PowerShell already having started up.

## Touchpad Toggle!
After I did all of this with my touchscreen, I identified which HID device was behind my touchpad and wrote a script to toggle that too, because why not:
```batch
devcon status "HID\MSHW0092&COL02" | find /i "Driver is running.">nul
if not %errorlevel%==1 (
	devcon disable "HID\MSHW0092&COL02"
) else (
	devcon enable "HID\MSHW0092&COL02"
)
```
This was saved to [`C:\Windows\TST\TPT.bat`](https://github.com/smcclennon/old-projects/blob/master/touchscreen_toggle/TPT.bat), and the following line was appended to `DriverToggle.ahk`:
```
RAlt & F11::Run, C:\Windows\TST\TPT.bat
```
I decided not to do the same for my keyboard HID..
## Conclusion
This was a very fun project to work on, and may be one of the most useful personal tools I have created. I've since moved to daily driving Linux, so I'm unable to check if these scripts still work, nor can I perform testing to remember specifics such as how exactly I went about finding the correct HID for my touchscreen.

The same touchscreen has unfortunately started experiencing phantom tapping so I've disabled it, and I won't be making a linux version of this solution because of that.

<a href="https://www.vecteezy.com/free-vector/vector">Vector Vectors by Vecteezy</a>